我正在尝试在VBA中创建一个宏,它将在“PasteSheet”中搜索包含“转换”一词的任何单元格。一旦我有了这个单元格,我就可以在与该行对应的其他列中收集其他信息。我遇到的问题是创建一些类型的循环,它将遍历整个数据库以返回单词“conversion”的所有实例。到目前为止,这是我的代码:
Sub Conversion()
Dim Comment As Range
Dim i As String
Worksheets("PasteSheet").Activate
Range("Comment").Find("conversion").Select
Worksheets("sheet1").Range("a1") = Selection.Offset(0, -8)
End Sub
请帮忙!
答案 0 :(得分:0)
这是你可以做到的一种方式。这将在数组中保存找到的单词的地址,您可以在最后使用该数组。我使用D列作为我的示例列。根据需要更改任何内容
Sub getCells()
Dim rng As Range, cel As Range
Dim celAddress() As Variant
Dim i As Long
i = 0
Set rng = Range("D1:D" & Cells(Rows.Count, 4).End(xlUp).Row)
ReDim cellAddress(rng.Cells.Count)
For Each cel In rng
If cel.Value = "conversion" Then
cellAddress(i) = cel.Address
i = i + 1
End If
Next cel
ReDim Preserve cellAddress(i - 1)
For i = LBound(cellAddress) To UBound(cellAddress)
' Do whatever with each cell address found
Debug.Print cellAddress(i)
Next i
End Sub