傳回項目在陣列中的相對位置,其符合指定順序中的指定值。 當您需要項目在某個範圍內的位置,而不是項目本身時,請使用 [相符 ] 而不是其中一個 [查閱 ] 函數。
語法
運算式。arg1、arg2、arg3) (相符項目
運算式 代表 WorksheetFunction 物件的變數。
參數
| 名稱 | 必要/選用 | 資料類型 | 描述 |
|---|---|---|---|
| Arg1 | 必要 | Variant | Lookup_value:您用來尋找所需值之資料表的值。 |
| Arg2 | 必要 | Variant | Lookup_array:包含可能查閱值的連續儲存格範圍。 Lookup_array 必須是陣列或陣列參照。 |
| Arg3 | 選用 | Variant | Match_type:數字 -1、0 或 1。 Match_type 會指定 Microsoft Excel 如何比對 lookup_value 與 lookup_array 中的值。 |
傳回值
雙精確度
註解
Lookup_value 是您想要在 lookup_array 中比對的值。 例如,當您在電話簿中查詢號碼時,您使用人員的名稱做為查閱值,但電話號碼卻是您想要的值。
Lookup_value 可以是值 (數字、文字、邏輯值),或是數字、文字、邏輯值的儲存格參照。
如果match_type為 1,Match 會尋找小於或等於 lookup_value 的最大值。 Lookup_array 必須以遞增次序排列:...,-2,-1,0,1,2,...A-Z,FALSE,TRUE。
如果 match_type 為 0,Match 會尋找完全等於 lookup_value 的第一個值。 Lookup_array 可以依任意次序排列。 請注意, Match 不區分大小寫。
如果 match_type 為 -1, 則比對 會尋找大於或等於 lookup_value 的最小值。 Lookup_array 必須以遞減順序排列:TRUE, FALSE, Z-A, ...2, 1, 0, -1, -2, ..., 等。
如果省略 match_type,則會假設其值為 1。
[相符] 會傳回相符值在lookup_array內的位置,而不是值本身。 例如, MATCH("b",{"a","b","c"},0) 傳回 2,即 “b” 在陣列 {"a","b","c"}中的相對位置。
比對文字值時,[比對] 不會區分大小寫字母。
如果 [相符] 找不到相符項目,則會傳回 #N/A 錯誤值。
如果 match_type 為 0,而且 lookup_value 為文字,您便可以在 lookup_value 中使用問號 (?) 及星號 (*) 萬用字元。 問號會符合任何單一字元,星號則會符合任何字元序列。 如果想要尋找實際的問號及星號,請在字元前面輸入波狀符號 (~)。
範例
對於第一份工作表之第一個資料行中的每個值,此範例會搜尋整個活頁簿來找出相符的值。 如果巨集發現相符的值,它會將第一份工作表中的原始值設定為粗體。
Sub HighlightMatches()
Application.ScreenUpdating = False
'Declare variables
Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long, bln As Boolean
'Set up the count as the number of filled rows in the first column of Sheet1.
iRowL = Cells(Rows.Count, 1).End(xlUp).Row
'Cycle through all the cells in that column:
For iRow = 1 To iRowL
'For every cell that is not empty, search through the first column in each worksheet in the
'workbook for a value that matches that cell value.
If Not IsEmpty(Cells(iRow, 1)) Then
For iSheet = ActiveSheet.Index + 1 To Worksheets.Count
bln = False
var = Application.Match(Cells(iRow, 1).Value, Worksheets(iSheet).Columns(1), 0)
'If you find a matching value, indicate success by setting bln to true and exit the loop;
'otherwise, continue searching until you reach the end of the workbook.
If Not IsError(var) Then
bln = True
Exit For
End If
Next iSheet
End If
'If you don't find a matching value, don't bold the value in the original list;
'if you do find a value, bold it.
If bln = False Then
Cells(iRow, 1).Font.Bold = False
Else
Cells(iRow, 1).Font.Bold = True
End If
Next iRow
Application.ScreenUpdating = True
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。