メインコンテンツへスキップ

ExcelでXNUMXつの文字列の類似性を比較したり、違いを強調したりするにはどうすればよいですか?

場合によっては、文字列のXNUMXつの隣接するセルを比較し、Excelでそれらの類似点または相違点をマークするだけでよい場合があります。 この記事では、それを実現するためのXNUMXつの方法を紹介します。

XNUMXつの文字列を式と比較します
XNUMXつの文字列の類似性を比較するか、VBAコードとの違いを強調します


XNUMXつの文字列を式と比較します

以下のスクリーンショットに示すように、比較された文字列が一致するかどうかを知りたいだけの場合は、次の式を適用できます。

1.空白のセルC2を選択し、数式を入力します = EXACT(A2、B2) 数式バーに移動し、Enterキーを押します。 スクリーンショットを参照してください:

Note:数式で、A2とB2は比較文字列を含むセルです。

2.結果セルを選択し続け、比較されたすべての結果が得られるまで、塗りつぶしハンドルをセルにドラッグします。

FALSEの結果は、比較された文字列が異なることを意味し、TRUEの結果は、XNUMXつの比較された文字列が一致したことを示します。 スクリーンショットを参照してください:


XNUMXつの文字列の類似性を比較するか、VBAコードとの違いを強調します

XNUMXつの文字列を比較して、それらの類似点または相違点を強調したい場合。 次のVBAコードが役立ちます。

1。 押す 他の + F11 キーを同時に開いて アプリケーション向け Microsoft Visual Basic 窓。

2。 の中に アプリケーション向け Microsoft Visual Basic ウィンドウ、クリック インセット > モジュール。 次に、次のコードをコピーしてコードウィンドウに貼り付けます。

VBAコード:XNUMXつの列文字列を比較して、類似性またはハイライトの違いを確認します

Sub highlight()
    Dim xRg1 As Range
    Dim xRg2 As Range
    Dim xTxt As String
    Dim xCell1 As Range
    Dim xCell2 As Range
    Dim I As Long
    Dim J As Integer
    Dim xLen As Integer
    Dim xDiffs As Boolean
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
lOne:
    Set xRg1 = Application.InputBox("Range A:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg1 Is Nothing Then Exit Sub
    If xRg1.Columns.Count > 1 Or xRg1.Areas.Count > 1 Then
        MsgBox "Multiple ranges or columns have been selected ", vbInformation, "Kutools for Excel"
        GoTo lOne
    End If
lTwo:
    Set xRg2 = Application.InputBox("Range B:", "Kutools for Excel", "", , , , , 8)
    If xRg2 Is Nothing Then Exit Sub
    If xRg2.Columns.Count > 1 Or xRg2.Areas.Count > 1 Then
        MsgBox "Multiple ranges or columns have been selected ", vbInformation, "Kutools for Excel"
        GoTo lTwo
    End If
    If xRg1.CountLarge <> xRg2.CountLarge Then
       MsgBox "Two selected ranges must have the same numbers of cells ", vbInformation, "Kutools for Excel"
       GoTo lTwo
    End If
    xDiffs = (MsgBox("Click Yes to highlight similarities, click No to highlight differences ", vbYesNo + vbQuestion, "Kutools for Excel") = vbNo)
    Application.ScreenUpdating = False
    xRg2.Font.ColorIndex = xlAutomatic
    For I = 1 To xRg1.Count
        Set xCell1 = xRg1.Cells(I)
        Set xCell2 = xRg2.Cells(I)
        If xCell1.Value2 = xCell2.Value2 Then
            If Not xDiffs Then xCell2.Font.Color = vbRed
        Else
            xLen = Len(xCell1.Value2)
            For J = 1 To xLen
                If Not xCell1.Characters(J, 1).Text = xCell2.Characters(J, 1).Text Then Exit For
            Next J
            If Not xDiffs Then
                If J <= Len(xCell2.Value2) And J > 1 Then
                    xCell2.Characters(1, J - 1).Font.Color = vbRed
                End If
            Else
                If J <= Len(xCell2.Value2) Then
                    xCell2.Characters(J, Len(xCell2.Value2) - J + 1).Font.Color = vbRed
                End If
            End If
        End If
    Next
    Application.ScreenUpdating = True
End Sub

3。 プレス F5 コードを実行するためのキー。 最初に Kutools for Excel ダイアログボックスで、比較する必要のあるテキスト文字列の最初の列を選択し、[ OK

4.次にXNUMX番目 Kutools for Excel ダイアログボックスが表示されます。XNUMX番目の列の文字列を選択して、 OK

5.最後に Kutools for Excel ダイアログボックスで、文字列の類似性を比較する場合は、[ 有り ボタン。 比較した文字列の違いを強調するには、 いいえ ボタン。 スクリーンショットを参照してください:

次に、以下のスクリーンショットに示すように、比較結果を確認できます。


関連記事:

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性を 50% 向上させ、毎日何百回もマウス クリックを減らすことができます!
Comments (21)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Is there an idiot's guide to Kutools anywhere?
This comment was minimized by the moderator on the site
Hi Sandy,

You can find the guide for each Kutools feature on this page: Kutools for Excel All Features Tutorial
This comment was minimized by the moderator on the site
Hi,
How can i edit the code such that it will compare 2 columns and highlight the differences in both columns?
Eg:
Column 1
A1,A2,A3,A4: Highlight A1 & A3
Column 2
A2,A4,A5,A6,A7: Highlight A5,A6,A7
This comment was minimized by the moderator on the site
Hi Lala,
It would be nice if you could upload a screenshot of your data and the result you want to get.
This comment was minimized by the moderator on the site
Confrontando la cella VIA ROMA 1 con la cella VIA RROMA 1 il programma evidenzia in rosso sia RROMA che 1. Non si può fare in modo che evidenzi solo la lettera diversa e quindi R? Grazie
This comment was minimized by the moderator on the site
in your example, words "lets try" is similar but your code can't highlight it.
This comment was minimized by the moderator on the site
To avoid any trouble of this code with the spaces which should be in ASCII value 32 or 160 (especially if the string comes from HTML) you need to include the test line 46 in a if endif loop as below:
If Not ((Asc(xCell1.Characters(J, 1).Text) = 32 Or Asc(xCell1.Characters(J, 1).Text) = 160) And (Asc(xCell2.Characters(J, 1).Text) = 32 Or Asc(xCell2.Characters(J, 1).Text) = 160)) Then
( line 46 If Not xCell1.Characters(J, 1).Text = xCell2.Characters(J, 1).Text Then Exit For)
End If

Nevertheless thanks for the code.
This comment was minimized by the moderator on the site
How do I add this properly? I am getting an error when I tried to replace line 46. Thank you
This comment was minimized by the moderator on the site
THANK YOU! this is just what i needed!
This comment was minimized by the moderator on the site
I was so hoping this would work. It appears to compare whole cells and not characters within the cells. Simply comparing Candy in one cell to Andy in another finds no similarities. And when I compare for differences the entire name Andy is highlighted in red.
This comment was minimized by the moderator on the site
First off..THANKS Extend Office folks for the good code!!

Try this modification. It simply expands the per character checking loop to include the font color formatting, adds a default "Black", and also compares characters out to the longest of the two cells being compared.

Sub highlight()
    Dim xRg1 As Range
    Dim xRg2 As Range
    Dim xTxt As String
    Dim xCell1 As Range
    Dim xCell2 As Range
    Dim I As Long
    Dim J As Integer
    Dim xLen As Integer
    Dim xDiffs As Boolean
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
lOne:
    Set xRg1 = Application.InputBox("Range A:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg1 Is Nothing Then Exit Sub
    If xRg1.Columns.Count > 1 Or xRg1.Areas.Count > 1 Then
        MsgBox "Multiple ranges or columns have been selected ", vbInformation, "Kutools for Excel"
        GoTo lOne
    End If
lTwo:
    Set xRg2 = Application.InputBox("Range B:", "Kutools for Excel", "", , , , , 8)
    If xRg2 Is Nothing Then Exit Sub
    If xRg2.Columns.Count > 1 Or xRg2.Areas.Count > 1 Then
        MsgBox "Multiple ranges or columns have been selected ", vbInformation, "Kutools for Excel"
        GoTo lTwo
    End If
    If xRg1.CountLarge <> xRg2.CountLarge Then
       MsgBox "Two selected ranges must have the same numbers of cells ", vbInformation, "Kutools for Excel"
       GoTo lTwo
    End If
    xDiffs = (MsgBox("Click Yes to highlight similarities, click No to highlight differences ", vbYesNo + vbQuestion, "Kutools for Excel") = vbNo)
    Application.ScreenUpdating = False
    xRg2.Font.ColorIndex = xlAutomatic
    For I = 1 To xRg1.Count
        Set xCell1 = xRg1.Cells(I)
        Set xCell2 = xRg2.Cells(I)
        If xCell1.Value2 = xCell2.Value2 Then
            If Not xDiffs Then xCell2.Font.Color = vbRed
        Else
            xLen = Application.WorksheetFunction.Max(Len(xCell1.Value2), Len(xCell2.Value2))
            For J = 1 To xLen
                xCell2.Characters(J, Len(xCell2.Value2) - J + 1).Font.Color = vbBlack
                If Not xCell1.Characters(J, 1).Text = xCell2.Characters(J, 1).Text Then
                    If Not xDiffs Then
                        If J <= Len(xCell2.Value2) And J > 1 Then
                            xCell2.Characters(1, J - 1).Font.Color = vbRed
                        End If
                    Else
                        If J <= Len(xCell2.Value2) Then
                            xCell2.Characters(J, Len(xCell2.Value2) - J + 1).Font.Color = vbRed
                        End If
                    End If
                End If
            Next J
        End If
    Next
    Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
just want to say thanks!
This comment was minimized by the moderator on the site
How can I get difference number between two strings?

Example: "123456" and "213456" ==> 2 difference
This comment was minimized by the moderator on the site
Very useful script! thanks
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations