Application.match in VBA

前提是你 提交于 2019-12-24 10:04:05

问题


I'm using "Application.match" to find if Column AD of a sheet contains "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".

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".

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.

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

回答1:


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.




回答2:


The problem is the way you use Application.Match. It should be like this:

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://stackoverflow.com/questions/44159529/application-match-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!