匹配功能?VBA 无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20028769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:30:08  来源:igfitidea点击:

Match function? VBA not working properly

excelfunctionvbamatch

提问by user2703472

I have a following code and i have "name" in cells B11 , B19 and B25 and i was expecting a result of 19 or 25 with the code but it returns Y=1. Could anyone tell me what is the problem?.

我有以下代码,并且在单元格 B11、B19 和 B25 中有“名称”,我期望代码的结果为 19 或 25,但它返回 Y=1。谁能告诉我是什么问题?

Sub mat()

Y = Application.Match("name", ActiveSheet.Range("B19:B30"), 0)
MsgBox Y

End Sub

回答by jpw

According to the documentation for MATCH:

根据MATCH文档

MATCH returns the position of the matched value within lookup_array, not the value itself.

MATCH 返回匹配值在 lookup_array 中的位置,而不是值本身。

and with 0as the optional third argument (match_type):

0作为可选的第三个参数 ( match_type):

If match_type is 0, MATCH finds the first value that is exactly equal to lookup_value. Lookup_array can be in any order.

如果 match_type 为 0,MATCH 会找到与 lookup_value 完全相等的第一个值。Lookup_array 可以按任何顺序排列。

So the returned 1refers to the position on B19in the array Range("B19:B30")and the code sample is indeed behaving as expected.

所以返回的1指的B19是数组中的位置,Range("B19:B30")代码示例确实按预期运行。

回答by Mark Fitzgerald

Application.Match("name", ActiveSheet.Range("B19:B30"), 0)

The MATCH function searches for a specified item in a range of cells, and then returns the relative positionof that item in the range

MATCH 函数在单元格范围内搜索指定的项目,然后返回该项目在该范围内的相对位置

So parsing the parameter ActiveSheet.Range("B19:B30")means that B19equals relative position=1.

所以解析参数ActiveSheet.Range("B19:B30")意味着B19等于相对位置=1

回答by ayadprof

add slgn

添加登录

Sub mat()
    Y = Application.Match(slng(range("a4").value), ActiveSheet.Range("B19:B30"), 0)
    MsgBox Y
End Sub

ex2:

例2:

Application.WorksheetFunction.Match(CLng(TextBox1.Text), sheet110.Range("B6:B" & ls), 0)