请问如何使用excel vba 查找2次,或多条件查找

如下代码,我想要先找到“S56”的位置,然后再向下查找“S57”,查找两次中间需要加入什么代码,以下的代码执行会出错。(以下代码只是程序中的一部分)

Cells.Find(What:="S56", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, MatchByte:=False, SearchFormat:=False).Activate

Cells.Find(What:="S57", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, MatchByte:=False, SearchFormat:=False).Activate

另,如果我要查找A列中单元格为“S56”,B列中单元格为“S57”的同一行,并显示那一行C列对应的单元格内容,如何实现,谢谢!
请看以下代码如何修改?
If Selection.Areas.Count <> 2 Then
MsgBox "请选择2个区域"
Exit Sub
Else
TheArea1 = Selection.Areas(1)
TheArea2 = Selection.Areas(2)
End If

R = Range("A65536").End(xlUp).Row
arr = Range("A1:C" & R)
For i = 1 To R
If arr(i, 1) = TheArea1 And arr(i, 2) = TheArea2 Then
MsgBox arr(i, 3)
n = n + 1
Else
End If
Next

在E1起输出结果

Sub test()
Set d = CreateObject("Scripting.Dictionary")
R = Range("A65536").End(xlUp).Row
arr = Range("A1:C" & R)
For i = 1 To R
  If arr(i, 1) = "s56" And arr(i, 2) = "s57" Then
  n = n + 1
  d(arr(i, 1) & "," & arr(i, 2) & "," & arr(i, 3)) = i
  End If
Next
[E1].Resize(d.Count) = Application.Transpose(d.keys)
[E1].Resize(d.Count).TextToColumns Comma:=True
End Sub

追问

请看下我最上面的补充,按你的想法,如果查找的是具体文本就可以实现,但是如果像我上面那个,查找的是单元格里的内容,就会出现错误,请问怎么修改其查找格式才能实现,谢谢

追答

将上面的改一下就可以用单元格来代替具体文本
如 If arr(i, 1) = [D1] And arr(i, 2) = [D2] Then 这是查找D1、D2单元格的内容
也可以象你那样改成TheArea1 、TheArea2

你补充的代码
用MsgBox来显示的话没什么大问题了
就是倒数3、4行的 n = n + 1 与 Else 是多余的
删掉即可

温馨提示:答案为网友推荐,仅供参考
相似回答