返回在指定方式下与指定数值匹配的数组中元素的相应位置。 当需要某个区域中某个项目的位置而不是项目本身时,请使用 Match 而不是其中一个 Lookup 函数。
语法
表达式。匹配 (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 中的值进行匹配。 |
返回值
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,即“b”在数组 {"a","b","c"}中的相对位置。
匹配文本值时,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 支持和反馈,获取有关如何接收支持和提供反馈的指南。