Skip i'r prif gynnwys

Sut i gymharu dau dant ar gyfer tebygrwydd neu dynnu sylw at wahaniaethau yn Excel?

Mewn rhai achosion, efallai mai dim ond dwy gell llinynnau gyfagos y bydd angen i chi eu cymharu a marcio eu tebygrwydd neu wahaniaethau yn Excel. Mae'r erthygl hon yn darparu dau ddull i chi ei gyflawni.

Cymharwch ddau dant â fformiwla
Cymharwch ddau dant ar gyfer tebygrwydd neu amlygwch wahaniaethau â chod VBA


Cymharwch ddau dant â fformiwla

Fel y dangosir isod y screenshot a ddangosir, os ydych chi eisiau gwybod a yw'r llinynnau o'u cymharu yn cyfateb ai peidio, gallwch gymhwyso'r fformiwla ganlynol.

1. Dewiswch gell wag C2, nodwch y fformiwla = EXACT (A2, B2) i mewn i'r Bar Fformiwla, ac yna pwyswch y fysell Enter. Gweler y screenshot:

Nodyn: Yn y fformiwla, A2 a B2 yw'r celloedd sy'n cynnwys y tannau cymharu.

2. Daliwch i ddewis y gell canlyniad, yna llusgwch y Llenwi Trin i'r celloedd nes cael yr holl ganlyniadau cymhariaethol.

Mae'r canlyniad ANWIR yn golygu bod y llinynnau o'u cymharu yn wahanol, ac mae'r canlyniad GWIR yn nodi bod y ddau dant o'u cymharu yn cyfateb. Gweler y screenshot:


Cymharwch ddau dant ar gyfer tebygrwydd neu amlygwch wahaniaethau â chod VBA

Os ydych chi am gymharu dau dant ac amlygu'r tebygrwydd neu'r gwahaniaethau rhyngddynt. Gall y cod VBA canlynol eich helpu chi.

1. Gwasgwch Alt + F11 allweddi ar yr un pryd i agor y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

2. Yn y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr, cliciwch Mewnosod > Modiwlau. Yna copïwch a gludwch y cod canlynol i mewn i ffenestr y Cod.

Cod VBA: Cymharwch ddau dant colofn ar gyfer tebygrwydd neu amlygwch wahaniaethau

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. Gwasgwch y F5 allwedd i redeg y cod. Yn y cyntaf Kutools ar gyfer Excel blwch deialog, dewiswch y golofn gyntaf o dannau testun y mae angen i chi eu cymharu, ac yna cliciwch ar y OK botwm.

4. Yna'r ail Kutools ar gyfer Excel blwch deialog yn ymddangos, dewiswch dannau'r ail golofn, a chliciwch ar y OK botwm.

5. Yn yr olaf Kutools ar gyfer Excel blwch deialog, os ydych chi am gymharu llinynnau am debygrwydd, cliciwch y Ydy botwm. Ac i dynnu sylw at wahaniaethau'r tannau o'u cymharu, cliciwch y Na botwm. Gweler y screenshot:

Yna gallwch weld y canlyniadau cymhariaethol fel isod llun a ddangosir.


Erthyglau cysylltiedig:

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
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