【发布时间】:2015-09-30 14:51:35
【问题描述】:
我想在表格中搜索,例如
A B C
D E C
嘿嘿
A H C
对于价值观
“A”和“C”,然后将值“Value”放入已找到这两个值的行中的单元格中。
我对整个主题相当陌生,所以我首先在网上搜索以找到可以帮助我的代码片段。
Dim FirstAddress As String
Dim SecondAddress As String
Dim MyArr As Variant
Dim MyArr2 As Variant
Dim Rng As Range
Dim Row As Range
Dim I As Long
Dim B As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
MyArr = Array("A")
MyArr2 = Array("C")
With Sheets("Sheet1").Range("F:F")
.Offset(0, 27).ClearContents
For I = LBound(MyArr) To UBound(MyArr)
Set Rng = .Find(What:=MyArr(I), After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not Rng Is Nothing Then
FirstAddress = Rng.Address
Do
Set Rng2 = Rng.EntireRow.Find(What:=MyArr2(I), After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not Row Is Nothing Then
SecondAdress = Row.Address
Do
Rng.Offset(0, 27).Value = "Value"
Set Row = .FindNext(Row)
Set Rng = .FindNext(Rng)
Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
End If
Next I
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
它对我使用 .find 方法搜索一个值很有用,但我很难搜索包含这两个值的行。 (如果我想搜索多个值的数组,比如说所有在开头的 A 或 D,然后是第三列中的 C)
你有什么想法吗? 我不明白如何实现几个循环。
谢谢!
【问题讨论】:
-
看看这里,问题非常相似,您应该能够根据自己的需要调整解决方案:stackoverflow.com/questions/18262403/…
-
工作就像一个魅力!