我需要一个excel的vba逻辑是当单元格A中包含了单元格B的数据则将他们相同部分删除后的数据放在单元格C列
以下是一个简单的VBA代码,可以实现您所需的逻辑:
Sub RemoveDuplicates()
Dim lastRow As Long
Dim i As Long, j As Long
Dim str1 As String, str2 As String, str3 As String
'获取最后一行的行号
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
'遍历每一行
For i = 1 To lastRow
'获取单元格A和B的内容
str1 = Cells(i, "A").Value
str2 = Cells(i, "B").Value
'查找单元格B的内容是否包含在单元格A中
If InStr(str1, str2) > 0 Then
'将单元格A中包含单元格B内容的部分删除
str3 = Replace(str1, str2, "")
'将删除后的内容放入单元格C
Cells(i, "C").Value = str3
End If
Next i
End Sub
请将此代码复制到Excel VBA编辑器中的一个新模块中,并在需要的时候运行它。
原文地址: https://www.cveoy.top/t/topic/bh1I 著作权归作者所有。请勿转载和采集!