Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VBA excel中查找连续行中的两个值_Excel_Vba_Lookup - Fatal编程技术网

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

如何在VBA excel中查找连续行中的两个值,excel,vba,lookup,Excel,Vba,Lookup,你好,先生! 我可以问一个问题吗?我将如何/使用什么代码在vba中同时搜索两个值?例如,我想设置一个条件,如果在同一行中找到值“4AC”和日期“04/11/2013”,则该条件将变为“True”。 提前感谢,更多的力量 使用Excel的范围。在执行此检查的IsFound函数中查找函数,如下所示 Function IsFound(ByVal rngRowToSearch As Range, strSearchTerm1 as String, strSearchTerm2 As String) As

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

使用Excel的
范围。在执行此检查的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")

然后用
bln基金会做任何你想做的事

,包括你尝试过的内容和结果会很好。代码是否正常工作或您是否遇到错误?IF块可以简化为一行:
blnFound=Not(rngFind1为Nothing或rngFind2为Nothing)
。将其更改为以行作为输入的函数是一个好主意@PatricK感谢您提出的重构建议。自从我写VBA以来已经十年了,所以很明显有一些代码“生锈”。我现在更新了代码,请再次检查代码:)看起来不错@user3015762您能检查一下这是否适合您的需要吗?如果是,请标记为答案。