簡體   English   中英

VBA可以將一個單元格中的單詞逐字匹配到另一個單元格中的單詞

[英]VBA to Match word by word from a cell to word by word in an another cell

我正在嘗試將A列中的每個單詞與B列中的每個單詞進行匹配。換句話說,我想檢查A列中的單詞是否存在於B列中。如果是,則在Column中突出顯示相同的單詞B.

我已經嘗試了下面的代碼,但這看起來是完全匹配的。


Dim xStr As String
    Dim i, Y As Variant
    Dim M, j As Long

count = Range("A4", Range("A4").End(xlDown)).Rows.count For i = 4 To count + 3 xStr = Range("B" & i).value With Range("G" & i) .Font.ColorIndex = 1 For j = 1 To Len(.Text) If Mid(.Text, j, Len(xStr)) = xStr Then .Characters(j, Len(M)).Font.ColorIndex = 3 Next j End With Next i

范例:

列A#創可貼防水
B ##列強生(Johnson&Johnson)耐洗抗菌創可貼(瓶子)

A列中的3個單詞應在B列中突出顯示。

我假設您要比較B4中的單詞和G4,然后比較B5中的單詞和G5,依此類推-每對分別比較。

如果使用空格作為定界符Split單元格的內容,則您的單詞在數組中,並且可以比較每個數組元素。

Private Sub CompareWords()
    Dim xStr() As String
    Dim i As Long
    Dim x As Long, y As Long

    With ActiveSheet
        For i = 4 To .Cells(.Rows.Count, "B").End(xlUp).Row
            xStr = Split(.Cells(i, "B").Value, " ")
            With .Cells(i, "G")
                .Font.ColorIndex = 1
                For x = LBound(xStr()) To UBound(xStr())
                    For y = 1 To Len(.Text)
                        If Mid(.Text, y, Len(xStr(x))) = xStr(x) Then
                            .Characters(y, Len(xStr(x))).Font.ColorIndex = 3
                        End If
                    Next y
                Next x
            End With
        Next i
    End With
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM