Compare Data in Two Ranges Row by Row and Count Matches

Steviemac

New Member
Joined
May 24, 2021
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Hi there, first time poster, long time lurker

Hoping some kind soul can speed up this macro with a light speed modification

The data to check (InputData) is replaced many times at this part of the process and at the rate the InputData comes in is
proving to be a real bottleneck, the macro works fine, it's just that it takes 16 seconds to run each time, which is too long
Hopefully there is a better faster way (maybe a macro rewrite!)

Data is held in two data sets, both 5 columns across
InputData set is 56 rows (variable)
TaskPercentComplete set is 2000 rows (variable)

Every row in the 'InputData' table is compared with Each row in turn in the 'TaskPercentComplete' table
for matches - it only has to record whether there is a match of 3 values and/or a match of 4 values

Any help is very much appreciated

Thanks Ste


VBA Code:
Sub TaskPercentages()

Application.ScreenUpdating = False

Dim InputData As Long

Sheets("TaskPercentComplete").Range("J3:L10000").ClearContents

 InputData = 2
 
   Do While Not Sheets("TaskPercentComplete").Cells(InputData, 46).Value = ""
  
       TaskDataOutput = 3
                 
        Do While Not Sheets("TaskPercentComplete").Cells(TaskDataOutput, 3).Value = ""
       
            Dim rngA As Range: Set rngA = Range("C" & TaskDataOutput & ":G" & TaskDataOutput)
            Dim rngB As Range: Set rngB = Range("AT" & InputData & ":AX" & InputData)

            If Evaluate("SUMPRODUCT(COUNTIF(" & rngA.Address & "," & rngB.Address & "))") = 4 Then
            Sheets("TaskPercentComplete").Cells(TaskDataOutput, 11).Value = "80%"
            End If
           
            If Evaluate("SUMPRODUCT(COUNTIF(" & rngA.Address & "," & rngB.Address & "))") = 3 Then
            Sheets("TaskPercentComplete").Cells(TaskDataOutput, 10).Value = "60%"
            End If

        TaskDataOutput = (TaskDataOutput + 1)
     
       Loop
   
      InputData = (InputData + 1)
  
   Loop
  
  'nextonwardmacro
 
End Sub
 
Obviously the cost of checking both cells every iteration is more expensive than just running the evaluate.
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Yep, that seems to be the cost, I have searched the internet for ages looking for a solution

The 37% reduction in runtime you made is a massive increase - brilliant!

All I can say is thanks Fluff, your help has been invaluable
(if something comes to mind, let me know :)

Thanks mate
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
As I've nothing to test on, I don't know if this will be an improvement or not because of the multiple nested loops. But running everything in memory is normally a lot faster than looping through cells
VBA Code:
Sub Steviemac()
   Dim Iary As Variant, Oary As Variant, Nary As Variant
   Dim r As Long, c As Long, rr As Long, cc As Long, i As Long
   
   With Sheets("TaskPercentComplete")
      Iary = .Range("AT2").CurrentRegion.Value2
      Oary = .Range("C3").CurrentRegion.Value2
   End With
   ReDim Nary(1 To UBound(Oary), 1 To 2)
   
   For r = 1 To UBound(Iary)
      For rr = 1 To UBound(Oary)
         For c = 1 To 5
            For cc = 1 To 5
               If Iary(r, c) = Oary(rr, cc) Then i = i + 1
            Next cc
         Next c
         If i = 3 Then Nary(rr, 1) = "60%"
         If i = 4 Then Nary(rr, 2) = "80%"
         i = 0
      Next rr
   Next r
   Sheets("TaskPercentComplete").Range("J3").Resize(UBound(Oary), 2).Value = Nary
End Sub
 
Upvote 0
Fluff... I am blown away - literally - it now takes 0.33 seconds, tested many times and it's consistently 0.33 seconds
and that's including the timer!! - phenomenal piece of code

it's gone from 16 seconds to 9.7 seconds to three times a second

As you obviously don't have the sheet I tested the sheet for counting correctly
It's exact, the 60% and the 80% are counted bang on


It just needs a little tweak (I hope it's little)
The output is correct but placed two rows down from the matched data

60% count is correct - just two rows down from the match
1622466371900.png


Exactly the same with the 80%
1622466465609.png


many many thanks

Ste
 
Upvote 0
Does your output range start at C3?
 
Upvote 0
In that case I'm not sure why you are getting that. It outputs the % on the correct rows for me.
+Fluff 1.xlsm
CDEFGHJKLATAUAVAWAX
1
272977571160708
3141531371065880%19662264886601
48863111973866260%1415357810658
59267065998260053952598485867
62291646284232680%63194748769988
76101351255668156426172751164
874243847769110618384620676278
98994288789196516229326462440
104104189265361254968396024328
Main
 
Upvote 0
Yours look good, it must be something my end, but it's definitely doing it

1622467989921.png


oh, that's interesting, let me wipe everything out and do it afresh
 
Upvote 0
Do you have any data in C1:G2 or AT1:AX1?
 
Upvote 0

Forum statistics

Threads
1,215,745
Messages
6,126,636
Members
449,324
Latest member
AmirMalik

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