Skip to main content

Conas dhá shraith a chur i gcomparáid lena chéile nó chun difríochtaí in Excel a aibhsiú?

I roinnt cásanna, b’fhéidir nach mbeidh ort ach dhá chealla téada cóngaracha a chur i gcomparáid lena chéile agus a gcosúlachtaí nó difríochtaí in Excel a mharcáil. Soláthraíonn an t-alt seo dhá mhodh chun tú a bhaint amach.

Déan comparáid idir dhá shraith leis an bhfoirmle
Déan comparáid idir dhá shraith le haghaidh cosúlachta nó aibhsigh difríochtaí le cód VBA


Déan comparáid idir dhá shraith leis an bhfoirmle

Mar a thaispeántar thíos an pictiúr, mura dteastaíonn uait ach a fháil amach an bhfuil na teaghráin chomparáideacha comhoiriúnaithe nó nach bhfuil, is féidir leat an fhoirmle seo a leanas a chur i bhfeidhm.

1. Roghnaigh cill bán C2, cuir isteach an fhoirmle = EXACT (A2, B2) isteach sa Bharra Foirmle, agus ansin brúigh an eochair Iontráil. Féach an pictiúr:

nótaí: San fhoirmle, is iad A2 agus B2 na cealla ina bhfuil na sreangáin chomparáideacha.

2. Coinnigh ort an chill toradh a roghnú, ansin tarraing an Láimhseáil Líon isteach chuig na cealla go dtí go bhfaighidh tú na torthaí comparáideacha go léir.

Ciallaíonn toradh FALSE go bhfuil na teaghráin chomparáideacha difriúil, agus léiríonn an toradh TRUE go bhfuil an dá shreang chomparáideach comhoiriúnaithe. Féach an pictiúr:


Déan comparáid idir dhá shraith le haghaidh cosúlachta nó aibhsigh difríochtaí le cód VBA

Más mian leat dhá shraith a chur i gcomparáid agus aird a tharraingt ar na cosúlachtaí nó na difríochtaí eatarthu. Is féidir leis an gcód VBA seo a leanas cabhrú leat.

1. Brúigh Eile + F11 eochracha ag an am céanna chun an Microsoft Visual Basic d’Fheidhmchláir fhuinneog.

2. Sa Microsoft Visual Basic d’Fheidhmchláir fuinneog, cliceáil Ionsáigh > Modúil. Ansin cóipeáil agus greamaigh an cód seo a leanas i bhfuinneog an Chóid.

Cód VBA: Déan comparáid idir dhá shraith cholún le haghaidh cosúlachta nó aibhsigh difríochtaí

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. Brúigh an F5 eochair chun an cód a rith. Sa chéad cheann Kutools le haghaidh Excel bosca dialóige, roghnaigh an chéad cholún de teaghráin téacs a chaithfidh tú a chur i gcomparáid, agus ansin cliceáil ar an OK cnaipe.

4. Ansin an dara ceann Kutools le haghaidh Excel tagann bosca dialóige aníos, roghnaigh teaghráin an dara colún, agus cliceáil an OK cnaipe.

5. Sa cheann deireanach Kutools le haghaidh Excel bosca dialóige, más mian leat teaghráin a chur i gcomparáid lena chéile, cliceáil an Is ea cnaipe. Agus chun aird a tharraingt ar dhifríochtaí na dtéad i gcomparáid, cliceáil ar an Uimh cnaipe. Féach an pictiúr:

Ansin is féidir leat na torthaí comparáideacha a fheiceáil mar atá thíos an pictiúr a thaispeántar.


Earraí gaolmhara:

Uirlisí Táirgiúlachta Oifige is Fearr

🤖 Kutools AI Aide: anailís sonraí a réabhlóidiú bunaithe ar: Forghníomhú Chliste   |  Gin Cód  |  Cruthaigh Foirmlí Saincheaptha  |  Anailís a dhéanamh ar Sonraí agus Cairteacha a Ghin  |  Feidhmeanna Kutools a agairt...
Gnéithe Coitianta: Faigh, Aibhsigh nó Aithnigh Dúblaigh   |  Scrios Sraitheanna Bána   |  Comhcheangail Colúin nó Cealla gan Sonraí a Chailleadh   |   Babhta gan Foirmle ...
Cuardaigh Super: Ilchritéir VLookup    VLookup Illuachanna  |   VLookup Trasna Ilbhileoga   |   Amharc doiléir ....
Liosta anuas Casta: Go tapa Cruthaigh Liosta Anuas   |  Liosta anuas Cleithiúnach   |  Liosta Buail Isteach Ilroghnacha ....
Bainisteoir Colún: Cuir Líon Sonrach Colún leis  |  Colúin Bog  |  Scoránaigh Stádas Infheictheachta na gColún Ceilte  |  Déan comparáid idir Raonta & Colúin ...
Gnéithe Réadmhaoin: Fócas Eangaí   |  Amharc Dearaidh   |   Barra Mór na Foirmle    Leabhar Oibre & Bainisteoir Bileog   |  Leabharlann Acmhainní (Uaththéacs)   |  Piocálaí Dáta   |  Comhcheangail Bileoga Oibre   |  Criptigh/Díchriptigh Cealla    Seol Ríomhphost trí Liosta   |  Scagaire Super   |   Scagaire Speisialta (scagaire trom/iodálach/stailc tríd...) ...
Barr 15 Uirlisí12 Téacs uirlisí (Cuir Téacs, Bain Carachtair,...)   |   50 + Cairt cineálacha (Cairt Gantt,...)   |   40+ Praiticiúil Foirmlí (Ríomh aois bunaithe ar lá breithe,...)   |   19 Insertion uirlisí (Cuir isteach Cód QR, Ionsáigh Pictiúr ón gCosán,...)   |   12 Tiontú uirlisí (Uimhreacha le Focail, Comhshó Airgeadra,...)   |   7 Cumaisc & Scoilt uirlisí (Sraitheanna Comhcheangail Casta, Cealla Scoilt,...)   |   ... agus eile

Supercharge Do Scileanna Excel le Kutools le haghaidh Excel, agus Éifeachtúlacht Taithí Cosúil Ná Roimhe. Kutools le haghaidh Excel Tairiscintí Níos mó ná 300 Ardghnéithe chun Táirgiúlacht a Treisiú agus Sábháil Am.  Cliceáil anseo chun an ghné is mó a theastaíonn uait a fháil ...

Tuairisc


Tugann Tab Oifige comhéadan Tabbed chuig Office, agus Déan Do Obair i bhfad Níos Éasca

  • Cumasaigh eagarthóireacht agus léamh tabbed i Word, Excel, PowerPoint, Foilsitheoir, Rochtain, Visio agus Tionscadal.
  • Oscail agus cruthaigh cáipéisí iolracha i gcluaisíní nua den fhuinneog chéanna, seachas i bhfuinneoga nua.
  • Méadaíonn do tháirgiúlacht 50%, agus laghdaíonn sé na céadta cad a tharlaíonn nuair luch duit gach lá!
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