在Excel 上,根据另一个工作表中列表的内容清除单元格bonCodigo 帮助我使用 VBA 宏脚本,该脚本具有指定的列和行范围以从 Sheet1 的 A 列中获取单词,然后在 Sheet2 列中找到它们作为完全匹配以获得发现清理干净了。结果在 Sheet3 中生成。
这是执行此操作的 VBA 代码:
Sub matchAndClear()
Dim ws As Worksheet
Dim arrKeys As Variant, arrData As Variant
Dim i As Integer, j As Integer, k As Integer
'-- here we take keys column from Sheet 1 into a 1D array
arrKeys = WorksheetFunction.Transpose(Sheets(1).Range("A1:A38").Value)
'-- here we take to be cleaned-up-range from Sheet 2 into a 2D array
arrData = WorksheetFunction.Transpose(Sheets(2).Range("A1:I100").Value)
'-- here we iterate through each key in keys array searching it in
'-- to-be-cleaned-up array
For i = LBound(arrKeys) To UBound(arrKeys)
For j = LBound(arrData, 2) To UBound(arrData, 2)
'-- when there's a match we clear up that element
If UCase(Trim(arrData(1, j))) = UCase(Trim(arrKeys(i))) Then
arrData(1, j) = " "
End If
'-- when there's a match we clear up that element
If UCase(Trim(arrData(2, j))) = UCase(Trim(arrKeys(i))) Then
arrData(2, j) = " "
End If
Next j
Next i
'-- replace old data with new data in the sheet 2 :)
Sheets(3).Range("A1").Offset(0, 0).Resize(UBound(arrData, 2), _
UBound(arrData)) = Application.Transpose(arrData)
End Sub
这次我需要一个稍微不同的 VBA 帮助。在 Sheet1 B 列中,这里是另一个单词列表,因此 VBA 不应查找并清除与 Sheet1 A 列中找到的单词列表值匹配的单元格内容,而是将找到的值(需要完全匹配)替换为 Sheet1 B 列中的值。