Sunjinsak
Board Regular
- Joined
- Jul 13, 2011
- Messages
- 151
- Office Version
- 365
- 2019
- 2016
- 2013
- Platform
- Windows
Hi,
I’ve got a problem here that is driving me INSANE!!!!!!!!!
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...
...to...
...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?
Many thanks!
I’ve got a problem here that is driving me INSANE!!!!!!!!!
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…
Code:
If Not masterArr(x, 1) = reportArr(y, 1) Then…
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!