我想在一张桌子内搜索,例如
A B C
D E C
A H I
A H C
对于值
" A"和" C"然后把价值"价值"进入已找到两个值的行中的单元格 我对整个主题相当新,所以我先在网上搜索,找到可以帮助我的代码片段。
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)
你知道吗? 我不知道如何实现几个循环。谢谢!
答案 0 :(得分:1)
从以下数据开始:
运行此宏:
Sub dural()
Dim N As Long, i As Long, A As String, C As String, v As String
Dim rng1 As Range, rng2 As Range, wf As WorksheetFunction
Set wf = Application.WorksheetFunction
A = "A"
C = "C"
v = "VALUE"
N = Cells(Rows.Count, A).End(xlUp).Row
Set rng1 = Range("A1:C" & N)
For i = 1 To N
Set rng2 = Intersect(Rows(i), rng1)
If wf.CountIf(rng2, A) > 0 And wf.CountIf(rng2, C) > 0 Then
Cells(i, "D") = v
End If
Next i
End Sub
将产生:
答案 1 :(得分:0)