如何在VBA excel的连续行中查找两个值

时间:2013-11-21 02:40:22

标签: excel vba lookup

好日子先生! 我可以问一个关于如何/用什么代码在vba中同时搜索两个值的问题?例如,如果在同一行中找到值“4AC”和日期“04/11/2013”​​,我想设置一个将变为“True”的条件。 提前谢谢,更有力量!

1 个答案:

答案 0 :(得分:1)

使用Excel的Range.Find函数,如下所示,执行此检查的IsFound函数。

Function IsFound(ByVal rngRowToSearch As Range, strSearchTerm1 as String, strSearchTerm2 As String) As Boolean

    Dim rngFind1 as Range, rngFind2 as Range

     ' try to find 1st value. If it exists, rngFind1 range will have the address. Else Nothing
    rngFind1 = rngRowToSearch.EntireRow.Find(What:=strSearchTerm1, LookIn:=xlValues, MatchCase:=False)

     ' try to find 2nd value. If it exists, rngFind1 range will have the address. Else Nothing
    rngFind2 = rngRowToSearch.EntireRow.Find(What:=strSearchTerm2, LookIn:=xlValues, MatchCase:=False)

    IsFound = Not (rngFind1 Is Nothing Or rngFind2 Is Nothing)

End Function

此外,您将按如下方式调用IsFound函数。

Dim blnFound as Boolean
blnFound = IsFound(Activesheet.Range("A1"),"4AC","04/11/2013")

然后对blnFound

做任何你想做的事情