Application.Match VBS
时间: 2023-10-11 08:08:32 浏览: 51
In VBS, the Application.Match function is used to search for a specified value in a range of cells and return its position. It has the following syntax:
```
Application.Match(lookup_value, lookup_range, [match_type])
```
- `lookup_value`: The value you want to search for.
- `lookup_range`: The range of cells where you want to search for the value.
- `match_type` (optional): The type of match you want to perform. It can be 1 (exact or next largest value), 0 (exact match), or -1 (exact or next smallest value).
The function returns the position of the found value within the range or an error if the value is not found.
Here's an example usage of the Application.Match function in VBS:
```vbs
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\path\to\your\workbook.xlsx")
Set objWorksheet = objWorkbook.Sheets("Sheet1")
lookupValue = "apple"
lookupRange = objWorksheet.Range("A1:A10")
result = objExcel.Application.Match(lookupValue, lookupRange, 0)
If Not IsError(result) Then
MsgBox "Found at position: " & result
Else
MsgBox "Value not found"
End If
objWorkbook.Close False
objExcel.Quit
```
This example searches for the value "apple" in the range A1:A10 of Sheet1 in an Excel workbook. If the value is found, it displays a message box with the position. Otherwise, it displays a message indicating that the value was not found.