我在Excel中有一个表格,我想创建一个宏,它检查列A中单元格中所有单元格B和C中的文本是否存在,然后从列A中删除匹配的单词。示例:宏将看到A1的文本中有红色和蓝色,B1和C1也是如此。然后它将删除A1中与B1 C1匹配的文本。然后宏将重复A2,B2,C2和A3,B3,C3。
当我尝试使用我的代码时,它会删除所有内容
A B C
1 redbluegreen | blue | red
2 greypinkblack| pink | grey
3 yellowpurple |purple| yellow
这是我的代码:
Sub Sort()
Dim A As String
Dim B As String
Dim C As String
Dim Result As String
Columns(1).Value = A
Columns(2).Value = B
Columns(3).Value = C
If A.contains(B) Or A.contains(C) Then
Result = InStr(1, B ,"")
A = Result
End If
End Sub
答案 0 :(得分:3)
答案 1 :(得分:0)
Sub Sort()
Dim cell As Range
For Each cell In Range("A1", Cells(Rows.Count, 1).End(xlUp))
cell.Value= Replace(Replace(cell.Text, cell.Offset(,1).Text, "") , cell.Offset(,2).Text, "")
Next
End Sub