1

我在单元格 A1 中有多个值,它们由“;”分隔。一些相同的值可能在单元格 B1 中。我需要使用单元格 B1 中的值搜索单元格 A1 中的值。所有未找到的值都需要显示在单元格 C1 中。

例如 - 单元格 A1 (Apple;Orange;Cherry) 单元格 B1 (Apple;Orange;) 单元格 c1 需要将“Cherry”反映为未找到

我试过这段代码:

Sub Splitvalue() 
    Dim str, mystr As Variant 
    Dim tp As Integer 
    str = Split(Range("A1").Value, ";") 
    For tp = LBound(str) To UBound(str) 
        mystr = str(tp) 
    Next 
End Sub
4

3 回答 3

0

为什么不像拆分第一个单元格那样拆分第二个单元格?然后看看是否在B1中找到A1的每个元素,否则输出到C1?

这并不优雅,但会起作用:

Sub Splitvalue()
    Dim str, mystr As Variant
    Dim stri As Variant
    Dim tp As Integer
    str = Split(Range("A1").Value, ";")
    str2 = Split(Range("B1").Value, ";")
    For tp = LBound(str) To UBound(str)
        mystr = str(tp)
        'Debug.Print mystr
        Dim found As Boolean
        found = False
        For Each stri In str2
            'Debug.Print stri
            If stri = mystr Then
                found = True
            End If
        Next stri
        If found = False Then
            Debug.Print mystr
        End If
    Next
End Sub
于 2013-06-12T11:04:20.483 回答
0

像这样设置你的 sheet1

设置表1

使用此代码

Option Explicit

Sub Splitvalue()

   Dim lastRow As Long
   lastRow = Range("A" & Rows.Count).End(xlUp).Row

   Dim c As Range
   Dim A As Variant, B As Variant
   Dim i As Long, j As Long
   Dim x As Boolean

   Columns(3).ClearContents

   For Each c In Range("A1:A" & lastRow)
      A = Split(c, ";")
      B = Split(c.Offset(0, 1), ";")
      For i = LBound(A) To UBound(A)
         For j = LBound(B) To UBound(B)
            If A(i) = B(j) Then
               x = True
               Exit For
            Else
               x = False
            End If
         Next j
         If Not x Then
            If IsEmpty(c.Offset(0, 2)) Then
               c.Offset(0, 2) = A(i)
            Else
               c.Offset(0, 2).Value = c.Offset(0, 2).Value & ";" & A(i)
            End If
         End If
      Next i
   Next

End Sub

你的结果应该是这样的 结果

于 2013-06-12T11:17:53.183 回答
0

单程:

dim needle() as string: needle = split(Range("B1").Value, ";")
dim haystack as string: haystack = ";" & Range("A1").Value & ";"
dim i as long

for i = 0 To ubound(needle)
    haystack = replace$(haystack, ";" & needle(i) & ";", ";")
next

If len(haystack) = 1 then haystack = ";;"
Range("C1").Value = Mid$(haystack, 2, Len(haystack) - 2)
于 2013-06-12T11:45:27.977 回答