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 中的值进行匹配。

返回值

Double

备注

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 不区分大写字母和小写字母。

如果 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 支持和反馈,获取有关如何接收支持和提供反馈的指南。