WorksheetFunction.Match 方法 (Excel)

傳回項目在陣列中的相對位置,其符合指定順序中的指定值。 當您需要某個專案在範圍中的位置,而不是專案本身的位置時,請使用 Match 而非其中一個 查閱 函式。

語法

運算式 對 (Arg1Arg2Arg3)

表達 代表 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, Match 會尋找大於或等於 lookup_value 的最小值。 Lookup_array 必須以遞減順序排列:TRUE, FALSE, Z-A, ...2, 1, 0, -1, -2, ..., 等。

如果省略 match_type,則會假設其值為 1。

Match 會傳回lookup_array內相符值的位置,而不是值本身。 例如, 會 MATCH("b",{"a","b","c"},0) 傳回 2,這是陣列 {"a","b","c"} 內 「b」 的相對位置。

對文字值時,比對不會區分大寫和小寫字母。

如果 Match 找不到相符專案,則會傳回#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 支援與意見反應