VBA 中的 Application.match
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44159529/
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
Application.match in VBA
提问by Karen88
I'm using "Application.match" to find if Column AD of a sheet contains "1".
我正在使用“Application.match”来查找工作表的 AD 列是否包含“1”。
But whenever I try to run my code, "Else" condition is always True. Although I'm expecting it to enter the "If" condition, since Column AD has a cell with a value "1".
但是每当我尝试运行我的代码时,“其他”条件始终为真。尽管我希望它进入“If”条件,因为 AD 列有一个值为“1”的单元格。
The values of the cells in Column AD are mostly results of a formula, except for AD1, which has the string "Check if Titile&Author Match", and AD2, which has the value "0".
AD 列中单元格的值大多是公式的结果,但 AD1 的字符串为“检查标题和作者匹配”,AD2 的值为“0”。
What is the problem. What is the solution for it?
问题是什么。它的解决方案是什么?
I hope you can suggest a solution that would still use the ".match" method. This is because from my understanding ".match" method returns the relative position of the match in the range. And I plan to use that value later on in my code.
我希望你能提出一个仍然使用“.match”方法的解决方案。这是因为根据我的理解,“.match”方法返回匹配项在范围内的相对位置。我计划稍后在我的代码中使用该值。
Private Sub CmdLocateDta_Click()
Dim SearchColumn As Integer
If Not IsError(Application.Match(1, "AD:AD", 0)) Then
SearchColumn = Application.Match(1, "AD", 0)
MsgBox "Data has been located." & vbNewLine & _
"You can now input the Lending Information below."
Else
MsgBox "There seems to be no such book in the Database." & _
vbNewLine & "Please re-check your input."
End If
End Sub
回答by Max
You need to specify the range where to search in both Matches.
您需要指定在两个匹配项中搜索的范围。
Application.Match(1, ws.range("AD:AD"),0)
where ws is your worksheet.
其中 ws 是您的工作表。
回答by Vityata
The problem is the way you use Application.Match.
It should be like this:
问题在于您使用Application.Match. 应该是这样的:
If Not IsError(Application.Match(1, Range("AD:AD"), 0)) Then
If Not IsError(Application.Match(1, Range("AD:AD"), 0)) Then
See more here: https://msdn.microsoft.com/en-us/library/office/ff835873.aspx
在此处查看更多信息:https: //msdn.microsoft.com/en-us/library/office/ff835873.aspx

