VBA looping through two arrays to find entries that DON'T match?

Sunjinsak

Board Regular
Joined
Jul 13, 2011
Messages
151
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Hi,

I’ve got a problem here that is driving me INSANE!!!!!!!!! :banghead:

I have 2 lists of ID numbers; one on a ‘master’ sheet and one on a ‘report’ sheet. I need to list all IDs on the ‘master’ sheet that DO NOT have a corresponding entry on the ‘report’ sheet.

So far I’ve read the relevant lists from each sheet into arrays and am attempting to loop through to find any non matching entries. I have no problem finding entries that DO match (see the example code below – it works perfectly)… but I have no idea how to go about finding entries that DON’T match??

As soon as I change...
Code:
If masterArr(x, 1) = reportArr(y, 1) Then…
...to...
Code:
If Not masterArr(x, 1) = reportArr(y, 1) Then…
...in the below code I get very unexpected results – it finds the first entry that DOES match and then repeatedly returns it in an infinite loop. If I insert an Else clause strange things also happen.

I just can’t seem to visualise what’s happening inside the loops in my mind so I don’t really know how to go about doing this. I’m sure to minds greater than mine it’s a very simple thing to achieve and I'm missing something painfully obvious. Can anyone help?

Code:
Sub View_Outstanding_Reports(ByVal SheetIndex As Integer)
 
Dim masterArr As Variant    'An array to hold the IDs of all staff on the master list
Dim reportArr As Variant    'An array to hold the IDs of all submitted reports
 
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(SheetIndex)
 
Dim LRowReport As Long, LRowMaster As Long  'Variables to hold the number of the last row containing a value
 
LRowReport = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
LRowMaster = Sheets("MasterList").Range("A" & Sheets("MasterList").Rows.Count).End(xlUp).Row
 
masterArr = Sheets("MasterList ").Range("A2:A" & LRowMaster).Value
reportArr = ws.Range("B6:B" & LRowReport).Value
 
Dim col As Collection       'A collection to hold non-matching IDs
Set col = New Collection
 
 
Dim x As Integer, y As Integer
 
    For x = 1 To UBound(masterArr, 1)
   
        For y = 1 To UBound(reportArr, 1)
       
            If masterArr(x, 1) = reportArr(y, 1) Then
                MsgBox "Match found: " & masterArr(x, 1)
            End If
            
        Next y
       
    Next x
 
'Clean up
Set ws = Nothing
 
End Sub


Sub Test()
 
Call View_Outstanding_Reports(ActiveSheet.Index)
 
End Sub

Many thanks!
 
Many thanks pbornemeier that's very informative and now I see what your code is doing.

I'll stick with what I already have for now as it works very well with the limited amount of entries it has to go through (there should never be more than 250 entries on the master list maximum - but often much less) but I've read the Scripting Dictionary link you gave me with interest. It's certainly something I'll look to use where appropriate in the future.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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