VBA to count matches from 2 columns

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi
Need some VBA help

I have a sheet with over 5000 entries Column C contains a Date and Column F contains an ID code

I need to check that when the first part (first 10 digits) of the ID codes are identical that the date in Column C matches for all IDS where the first 10 digits match. If it doesn't match then maybe highlight it

Some IDs have 1 so they are ok but some have 2 some 3 and some 4 etc

e.g. because 1st 10 characters are the same and the date is the same then its ok
20/11/20 1234567890555
20/11/20 1234567890666

But even though these 4 IDs are ok there dates are different
14/11/20 1234567890888
14/11/20 1234567890123
15/11/20 1234567890891
16/11/20 1234567890716

Hope that makes sense

Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi. Try:
VBA Code:
Sub CountPartialMatches()
 Dim LR As Long
  LR = Cells(Rows.Count, 3).End(3).Row
  [G:G] = ""
  Range("G2:G" & LR) = "=SUMPRODUCT((C$2:C$" & LR & "=C2)*(LEFT(F$2:F$" & LR & ",10)=LEFT(F2,10)))"
  Range("G2:G" & LR).Value = Range("G2:G" & LR).Value
End Sub

The counting will be placed by the code in column G.
The results equal to 1 means that there is no matches for that record.
Then you could apply Auto Filter on column G.
 
Upvote 0
It partially works, see below, but if I add a number that doesn't match the 1st 10 digits, that also shows a 1

14/11/20 1234567890888. 2
14/11/20 1234567890123. 2
15/11/20 1234567890891. 1
16/11/20 1234567890716. 1
16/11/20 8888888888888. 1, because this one doesn't have any matches in that column I don't want it to show a 1

I just need the ones that have a count of greater than 1 to check with the date
I also noticed that if the 3rd and 4th date matched that would also appear as 2

Wherever the first 10 characters match, all the dates should be the same. If they don't match then show those

Maybe I over complicated it by using 1st 10 characters, maybe I could have just used left function to get first 10 first characters.

another example

14/11/20 123456
14/11/20 123456
15/11/20 123456
16/11/20 123456
17/11/20 888888

Only ones I want to see when filtered are the ones on the 15th and 16th as they appear in that column more than once but dates don't match. Also if they both appeared on the 15th that woul also show a 2. I want it to be highlighted as all 4 of them should be same date.
 
Upvote 0
Try the two new versions below and see if we're closer. Otherwise, please show us some data with the desired results.
VBA Code:
Sub CountPartialMatchesV2()
 Dim LR As Long
  LR = Cells(Rows.Count, 3).End(3).Row
  With Range("G2:G" & LR)
   .Value = ""
   .Interior.Color = xlNone
   .Value = "=SUMPRODUCT((C$2:C$" & LR & "=C2)*(LEFT(F$2:F$" & LR & ",10)=LEFT(F2,10)))"
   .Value = .Value
   .Replace 1, "", xlWhole
   .SpecialCells(xlBlanks).Interior.Color = vbYellow
  End With
End Sub

VBA Code:
Sub CountPartialMatchesV3()
 Dim LR As Long
  LR = Cells(Rows.Count, 3).End(3).Row
  With Range("G2:G" & LR)
   .Value = ""
   .Interior.Color = xlNone
   .Value = "=SUMPRODUCT((C3:C$" & LR & "=C2)*(LEFT(F3:F$" & LR & ",10)=LEFT(F2,10)))"
   .Value = .Value
   .Replace 0, "", xlWhole
   .SpecialCells(xlBlanks).Interior.Color = vbYellow
  End With
End Sub
 
Upvote 0
@rhombus4

I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Also, do you really need vba? What if this could be done with Conditional Formatting like below?

20 11 28.xlsm
CDEF
1DateID
219/06/20201234567890888
319/06/20201234567890777
419/06/20201234567890241
520/06/20209876543210235
618/06/20209876543210236
718/06/20205698563215487
819/06/2020986523021547
919/06/20201234567890241
1019/06/2020555555555555
1123/06/20209876543210237
1220/06/2020155555555555
1320/06/2020155555555550
1420/06/20205555555555870
Check ID & Date
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C2:C14Expression=SUMPRODUCT(--(LEFT(F$2:F$14,10)=LEFT(F2,10)),--(C$2:C$14<>C2))textNO
 
Upvote 0
Thanks guys.

Peter your conditional format worked apart from one thing. If you changed C11 on yours to from 23/06/20 to 18/06/20 that shouldn't be highlighted as C6 will have same date and same first 10 digits. The only one highlighted of the 3 with same case ID is C5 as even though the ID matches that one is 20/06/20 and other two will be 18/06/20

20/06/20209876543210235



Osvaldo, unfortunately couldn't get yours to work
1st formula =SUMPRODUCT((C$2:C$14=C2)*(LEFT(F$2:F$14,10)=LEFT(F2,10)))
2nd formula =SUMPRODUCT((C3:C$14=C2)*(LEFT(F3:F$14,10)=LEFT(F2,10)))


Using Peters example above the results of 1st formula were
4
4
4
1
2
1
1
4
1
2
2
2
1

2nd formula

3
2
1
0
1
0
0
0
0
0
1
0
1
 
Upvote 0
Peter your conditional format worked apart from one thing. If you changed C11 on yours to from 23/06/20 to 18/06/20 that shouldn't be highlighted as C6 will have same date and same first 10 digits. The only one highlighted of the 3 with same case ID is C5 as even though the ID matches that one is 20/06/20 and other two will be 18/06/20
That seems to contradict your original statement...
when the first part (first 10 digits) of the ID codes are identical that the date in Column C matches for all IDS where the first 10 digits match.
 
Upvote 0
So, what is your desired result ?
Only highlight ones that have more than one, where the date doesn't match.

Basically all IDs which are the same should have the same date. If 1 or more of them have a different date highlight these. If they all have a different date then you can highlight them all

If there are 2 with same ID and have same date then it's ok. If they have different dates, highlight both

If 3 have same ID. If 2 have same date and other one has different date, highlight that one. All 3 different dates highlight all 3

If 4 have same ID. If 3 have same date highlight other one. If they have 2 with one date and other 2 have different dates highlight those 2. If 2 have same date and other 2 have same date then can highlight all.
 
Upvote 0

Forum statistics

Threads
1,215,847
Messages
6,127,264
Members
449,372
Latest member
charlottedv

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