如何使用VBA从第1列下找到的字符串搜索中提取单元格引用列表?例如,从下面附带的图像中,如何从第1行返回字符串“Apple”的单元格引用列表?优选地,单元格引用的列表应列在第二列下。
提前谢谢。
答案 0 :(得分:0)
试试这个:
Option Compare Text 'to remove case sensitivity
Sub test()
Dim S1$, S2$, Fruits As Range, x As Range
Set Fruits = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
For Each x In Fruits
If x.Value2 = "Apple" Then
S1 = S1 & " " & x.Address(0, 0)
ElseIf x.Value2 = "Kiwi" Then
S2 = S2 & " " & x.Address(0, 0)
End If
Next
S1 = Mid(S1, 2, Len(S1)): S2 = Mid(S2, 2, Len(S2))
Range([B2], Cells(UBound(Split(S1)) + 2, "B")) = Application.Transpose(Split(S1))
Range([C2], Cells(UBound(Split(S2)) + 2, "C")) = Application.Transpose(Split(S2))
End Sub
试验: