嗨我有一个excel,其中一张A包含一个表,其中一列有逗号分隔值,另一张B有一些数据。 所以我需要我需要将数据从B搜索到工作表以逗号分隔的列,如果找到则从字符串中删除该数据。在这里,我需要根据另一列来处理这些列。例如:
表A
a | 1,2,3,4-
b | 1,3,5,7-
c | 1,2,3,4-
d | 1,3,5,7-
e | 1,2,3,4-
表B
a | 1
a | 2
b | 1
b | 3
c | 1
c | 4
d | 3
d | 7
结果:
a | 3,4
b | 5,7
c | 2,3
d | 1,5
e | 1,2,3,4-
是否可以获取结果?我是excel和宏的新手,但尝试过仍然可以找到确切的解决方案。是否使用宏或excel公式有最简单的方法。
答案 0 :(得分:0)
这对我有用。对于较大的数据集,效率可能是一个问题。
Sub Main()
Dim shtAList As Range, rngA As Range
Dim shtBList As Range, rngB As Range
Set shtAList = Worksheets("A").Range("A1:A5")
Set shtBList = Worksheets("B").Range("A1:A8")
For Each rngA In shtAList
For Each rngB In shtBList
If rngB = rngA Then
rngA.Offset(0, 1) = StripString(rngA.Offset(0, 1), rngB.Offset(0, 1))
End If
Next rngB
Next rngA
End Sub
Function StripString(csv As Range, strip_string As String) As String
Dim strings() As String, i As Integer, temp As String, result As String
temp = vbNullString
strings = VBA.Split(csv, ",")
For i = 0 To UBound(strings)
If strings(i) <> strip_string Then
temp = temp & strings(i) & ","
End If
Next i
result = IIf(VBA.Right$(temp, 1) = ",", VBA.Left$(temp, Len(temp) - 1), temp)
StripString = result
End Function
Main
只是循环遍历您的两个列表以查找匹配项。StripString
是字符串和剥离比较的地方