I have this macro that allows you to cross reference "Sheet2" in "Sheet1" where"Sheet1" is the sheet that will contain my master data. The idea here is to compare sheet 2 to the master data and see if it matches. The problem with this macro is that it only compares within a limited amount of range. I was wondering how to make this more dynamic or flexible should I add another column that would also be used for cross reference.
Here is the sample of my sheets.
Example:
Sheet1
Name ID Class Name Taken?
John Riley 0001 Painting Yes
Bob Johnson 0101 Painting No
Matthew Ward 1111 Math Yes
Sheet 2:
Name ID Class Name Taken?
Matthew Ward 1111 Math Yes
Bob Johnson 0101 Painting No
Warren Renner 2222 Drama No
John Riley 0001 Painting Yes
What do I need to change in the macro to make it compare should I add additional columns in my sheets?
Example:
Sheet1
Name ID Class Name Taken? Date Taken
John Riley 0001 Painting Yes 8/25/13
Bob Johnson 0101 Painting No
Matthew Ward 1111 Math Yes 9/20/10
Sheet 2:
Name ID Class Name Taken? Date Taken
Matthew Ward 1111 Math Yes 9/20/10
Bob Johnson 0101 Painting No -
Warren Renner 2222 Drama No -
John Riley 0001 Painting Yes 8/25/13
Code:
Sub Compare_Data()
Dim rngData2 As Range
Dim rngData1 As Range
Dim cell2 As Range
Dim cell1 As Range
Dim rLastCell As Range
Set rngData2 = Worksheets("Sheet2").Range("B3", Worksheets("Sheet2").Range("B65536").End(xlUp))
Set rngData1 = Worksheets("Sheet1").Range("B3", Worksheets("Sheet1").Range("B65536").End(xlUp))
' Check customers in "Sheet2" to "Sheet1"
For Each cell2 In rngData2
For Each cell1 In rngData1
With cell1
If .Offset(0, 0) = cell2.Offset(0, 0) And _
.Offset(0, 1) = cell2.Offset(0, 1) And _
.Offset(0, 2) = cell2.Offset(0, 2) And _
.Offset(0, 3) = cell2.Offset(0, 3) Then
.Offset(0, -1).Range("A1:F1").Interior.ColorIndex = 3
cell2.Offset(0, 4) = .Offset(0, 4)
End If
End With
Next cell1
Next cell2
End Sub