VBA compare two ranges of datasets on same worksheet and mark identical sets red and strike through

Eazyeagle

New Member
Joined
Jan 31, 2019
Messages
6
Hi there,


I'm new here and hope that any of you will be able to help me out.
I have a form that has a set of values that is kind of fixed (the range A in the table below) and a range that is dynamic (the range B in the table below(not always in the same collumns)




Range ARange B
1-2 7-15 16-X11-2
1-38-X117-X12-4
2-49-1018-X14-6
2-59-1119-X16-8
3-410-X120-228-X1
3-511-1220-23X1-X2
4-611-1321-X3X2-21
4-712-X122-2421-X3
5-613-X122-25X3-44
5-714-16 44-46
6-814-17 46-49
6-915-18 49-50
7-14 15-19 50-52
52-66
66-70
70-E1
E-999

<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>


Now I'd like to have the identical datasets within each range to be red and striked through


Like this:
Range ARange B
<strike>1</strike><strike>-</strike><strike>2</strike> 7-15 16-X1<strike>1</strike><strike>-</strike><strike>2</strike>
1-3<strike>8</strike><strike>-</strike><strike>X1</strike>17-X1<strike>2</strike><strike>-</strike><strike>4</strike>
<strike>2</strike><strike>-</strike><strike>4</strike>9-1018-X1<strike>4</strike><strike>-</strike><strike>6</strike>
2-59-1119-X1<strike>6</strike><strike>-</strike><strike>8</strike>
3-410-X120-22<strike>8</strike><strike>-</strike><strike>X1</strike>
3-511-1220-23X1-X2
<strike>4</strike><strike>-</strike><strike>6</strike>11-1321-X3X2-21
4-712-X122-2421-X3
5-613-X122-25X3-44
5-714-16 44-46
<strike>6</strike><strike>-</strike><strike>8</strike>14-17 46-49
6-915-18 49-50
7-14 15-19 50-52
52-66
66-70
70-E1
E-999

<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>



I've this vba script;
Sub FindMatch()


'First, we declare four Range objects and two variables of type Integer. '


Dim rangeToUse As Range, singleArea As Range, cell1 As Range, cell2 As Range, i As Integer, j As Integer
'2. We initialize the Range object rangeToUse with the selected range.'


Set rangeToUse = Selection
'3. Add the line which changes the background color of all cells to 'No Fill'. Also add the line which removes the borders of all cells.'


Cells.Interior.ColorIndex = 0
Cells.Borders.LineStyle = xlNone
'4. Inform the user when he or she only selects one area.'


If Selection.Areas.Count <= 1 Then
MsgBox "Please select more than one area."
Else




End If
'The next code lines (at 5, 6 and 7) must be added between Else and End If.


'5. Color the cells of the selected areas.


rangeToUse.Interior.ColorIndex = 0
'6. Border each area.


For Each singleArea In rangeToUse.Areas
singleArea.BorderAround ColorIndex:=1, Weight:=xlThin
Next singleArea
'7. The rest of this program looks as follows.


For i = 1 To rangeToUse.Areas.Count
For j = i + 1 To rangeToUse.Areas.Count
For Each cell1 In rangeToUse.Areas(i)
For Each cell2 In rangeToUse.Areas(j)
If cell1.Value = cell2.Value Then
cell1.Interior.ColorIndex = 45
cell2.Interior.ColorIndex = 45
End If
Next cell2
Next cell1
Next j
Next i
'From: https://www.excel-easy.com/vba/examples/compare-ranges.html


End Sub


It works. But I still have to strike through all the identical data sets manually
And I'd rather have the text to be red in stead of the complete cell to change to orange


I hope someone can help me out?!


Regards


EazyEagle
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi & welcome to MrExcel.
replace these two lines
Code:
cell1.Interior.ColorIndex = 45
                    cell2.Interior.ColorIndex = 45
with
Code:
   cell1.Font.color = vbRed
   cell1.Font.Strikethrough = True
   cell2.Font.color = vbRed
   cell2.Font.Strikethrough = True
 
Upvote 0
Hi & welcome to MrExcel.
replace these two lines
Code:
cell1.Interior.ColorIndex = 45
                    cell2.Interior.ColorIndex = 45
with
Code:
   cell1.Font.color = vbRed
   cell1.Font.Strikethrough = True
   cell2.Font.color = vbRed
   cell2.Font.Strikethrough = True

That's a stap ehead!
I'm pleased but...
The result is now...

Like this:
Range ARange B
<strike>1</strike><strike>-</strike><strike>2</strike>7-1516-<strike style="color: rgb(255, 0, 0);">X1</strike><strike>1</strike><strike>-</strike><strike>2</strike>
<strike style="color: rgb(255, 0, 0);">1</strike>-3<strike>8</strike><strike>-</strike><strike>X1</strike>17-<strike style="color: rgb(255, 0, 0);">X1</strike><strike>2</strike><strike>-</strike><strike>4</strike>
<strike>2</strike><strike>-</strike><strike>4</strike>9-1018-<strike style="color: rgb(255, 0, 0);">X1</strike><strike>4</strike><strike>-</strike><strike>6</strike>
<strike style="color: rgb(255, 0, 0);">2</strike>-59-1119-<strike style="color: rgb(255, 0, 0);">X1</strike><strike>6</strike><strike>-</strike><strike>8</strike>
3-<strike style="color: rgb(255, 0, 0);">4</strike>10-<strike style="color: rgb(255, 0, 0);">X1</strike>20-22<strike>8</strike><strike>-</strike><strike>X1</strike>
3-511-1220-23<strike style="color: rgb(255, 0, 0);">X1</strike>-X2
<strike>4</strike><strike>-</strike><strike>6</strike>11-1321-X3X2-21
<strike style="color: rgb(255, 0, 0);">4</strike>-712-<strike style="color: rgb(255, 0, 0);">X1</strike>22-2421-X3
5-<strike style="color: rgb(255, 0, 0);">6</strike>13-<strike style="color: rgb(255, 0, 0);">X1</strike>22-25X3-44
5-714-1644-46
<strike>6</strike><strike>-</strike><strike>8</strike>14-1746-49
<strike style="color: rgb(255, 0, 0);">6</strike>-915-1849-50
7-1415-1950-52
52-66
66-70
70-E1
E-999

<tbody>
</tbody>


That's not quite what I like.. We are close... But...
I'd like to see the exact matches between range A and range B...

So if in range B
1-2 is marked,
than mark in range A only 1-2!
and not 1- as well!!!

Thank you in advance...
 
Upvote 0
Thank you Fluff,

That works as expected ok!
Just one question... It marks all numbers that are equal in both ranges.
But I'm looking for the exact matching combinations within the ranges.

For example if in range B 1-2 is added, than I'm looking for the exact match in range A => 1-2
And not the half match 1- or -2

Any idea?
 
Upvote 0
If the code you posted doesn't work, why did you say
It works. But I still have to strike through all the identical data sets manually
?
 
Last edited:
Upvote 0
Because I'm happy that part of it works... What I coudn't work out before.
(I'm sorry for my Enlish and I'm just starting to work with VBA...) ;)
 
Upvote 0
Will the RangeB always be one area as shown?
 
Upvote 0
It will always be as shown.
The range B can be in a different set of collumns, but it will be as shown.

At this moment I'm working with a B range that is situated in the collumns DS to DU.
And the next B range will be in the collumns EB to ED, etc.
 
Upvote 0
Try
Code:
Sub Eazyeagle()
   Dim Ar As Areas
   Dim Cl As Range
   Dim Valu As String
   Dim i As Long
   
   Set Ar = Selection.Areas
   If Ar.Count <= 1 Then
      MsgBox "Please select more than one area."
      Exit Sub
   End If
   With Cells
      .Borders.LineStyle = xlNone
      .Font.color = vbBlack
      .Font.Strikethrough = False
   End With
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Ar(Ar.Count).Columns(1).Cells
         Valu = join(Application.Index(Cl.Resize(, 3).Value, 1, 0), "|")
         .Item(Valu) = Empty
      Next Cl
      For i = 1 To Ar.Count - 1
         Ar(i).BorderAround , xlThin, xlColorIndexAutomatic
         For Each Cl In Ar(i).Columns(1).Cells
            Valu = join(Application.Index(Cl.Resize(, 3).Value, 1, 0), "|")
            If .Exists(Valu) Then
               Cl.Resize(, 3).Font.color = vbRed
               Cl.Resize(, 3).Font.Strikethrough = True
            End If
         Next Cl
      Next i
   End With
End Sub
 
Upvote 0
This works perfect.
I've to change my collumns on the A range to one collumnset of 3collumns in stead of 4 collumns sets of 3collumns. But it works amazing.
Thank you very much!

Eazyeagle
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top