Vba 在单元格中搜索字符串

Vba 在单元格中搜索字符串,vba,excel,Vba,Excel,我在单元格A1中有多个值,它们之间用“;”分隔。单元格B1中可能存在一些相同的值。我需要使用单元格B1中的值搜索单元格A1中的值。所有未找到的值都需要显示在单元格C1中 Eg-苹果A1细胞;橙色樱桃细胞B1苹果;橙色单元格c1需要反映未找到的Cherry 我尝试了以下代码: Sub Splitvalue() Dim str, mystr As Variant Dim tp As Integer str = Split(Range("A1").Value, ";")

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

Eg-苹果A1细胞;橙色樱桃细胞B1苹果;橙色单元格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

为什么不像拆分第一个单元格那样拆分第二个单元格?然后查看是否在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

像这样把床单放好

用户可以使用此代码

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
你的结果应该是这样的 单向:

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)

你试过什么?发布你的代码,告诉我们什么不起作用。阅读关于将拆分函数作为起点的文章我无法在循环中工作。我试过这个代码,请检查Sub-Splitvalue Dim str,mystr作为变量Dim tp作为Integer str=SplitRangeA1.Value;对于tp=LBoundstr到UBoundstr mystr=strtp下一个端点子节点