Hi All,
Sorry for the long desc -
I have two ranges I want to compare. Range one is in Sheet1 and range two is in Sheet2. Both ranges share the same ID number(the ID numbers are not in the same order in in the worksheets and I can't sort them, because it will effect other data in the workbook).
For each ID in Sheet1 I want the macro to find the matching ID in Sheet2 and compare the data in that row to the data in Sheet1. In both ranges there are a total of 5 columns. Column 1 contains the Unique ID and the other columns contain key data points I want to compare.
If the macro finds any mistmatch I want it to spit the mismatch per unique ID to Sheet3. I have the below macro, but I can't figure out how to modify it to do what I just described. Any suggestions would be greatly appreciated.
Sub CompareSheets()
Dim shtA As Worksheet
Dim shtB As Worksheet
Dim shtE As Worksheet
Dim rng As Range
Dim a As Range
Dim b As Range
Dim nErrorOutRow As Long
Dim nFirstRow As Long
Dim nLastRow As Long
Dim nFirstCol As Integer
Dim nLastCol As Integer
' Update the following with the actual sheet names:
Set shtA = Worksheets("Sheet1")
Set shtB = Worksheets("Sheet2")
Set shtE = Worksheets("Sheet3")
' Determine rows and cols to test
With shtA.Range("one")
nFirstRow = .Cells(1, 1).Row
nLastRow = .Rows.Count + .Cells(1, 1).Row - 1
nFirstCol = .Cells(1, 1).Column
nLastCol = .Columns.Count + .Cells(1, 1).Column - 1
End With
With shtB.Range("two")
nFirstRow = WorksheetFunction.Min(nFirstRow, .Cells(1, 1).Row)
nLastRow = WorksheetFunction.Max(nLastRow, .Rows.Count + .Cells(1, 1).Row - 1)
nFirstCol = WorksheetFunction.Min(nFirstCol, .Cells(1, 1).Column)
nLastCol = WorksheetFunction.Max(nLastCol, .Columns.Count + .Cells(1, 1).Column - 1)
End With
Set rng = Range(Cells(nFirstRow, nFirstCol), Cells(nLastRow, nLastCol))
' Set up exceptions sheet
shtE.Cells.Clear
shtE.Cells(1, 1) = "Ref."
shtE.Cells(1, 2) = shtA.Name
shtE.Cells(1, 3) = shtB.Name
nErrorOutRow = 2
' Process all cells and compare
Application.ScreenUpdating = False
For Each a In shtA.Range(rng.Address)
Set b = shtB.Range(a.Address)
If a.Value <> b.Value Then
shtE.Cells(nErrorOutRow, 1) = b.Address(0, 0)
'shtE.Cells(nErrorOutRow, 1) = a.Value
shtE.Cells(nErrorOutRow, 2) = a.Value
shtE.Cells(nErrorOutRow, 3) = b.Value
nErrorOutRow = nErrorOutRow + 1
End If
Next a
Application.ScreenUpdating = True
shtE.Activate
' Clean up and terminate
Set shtA = Nothing
Set shtB = Nothing
Set shtE = Nothing
Set rng = Nothing
Set a = Nothing
Set b = Nothing
End Sub
Sorry for the long desc -
I have two ranges I want to compare. Range one is in Sheet1 and range two is in Sheet2. Both ranges share the same ID number(the ID numbers are not in the same order in in the worksheets and I can't sort them, because it will effect other data in the workbook).
For each ID in Sheet1 I want the macro to find the matching ID in Sheet2 and compare the data in that row to the data in Sheet1. In both ranges there are a total of 5 columns. Column 1 contains the Unique ID and the other columns contain key data points I want to compare.
If the macro finds any mistmatch I want it to spit the mismatch per unique ID to Sheet3. I have the below macro, but I can't figure out how to modify it to do what I just described. Any suggestions would be greatly appreciated.
Sub CompareSheets()
Dim shtA As Worksheet
Dim shtB As Worksheet
Dim shtE As Worksheet
Dim rng As Range
Dim a As Range
Dim b As Range
Dim nErrorOutRow As Long
Dim nFirstRow As Long
Dim nLastRow As Long
Dim nFirstCol As Integer
Dim nLastCol As Integer
' Update the following with the actual sheet names:
Set shtA = Worksheets("Sheet1")
Set shtB = Worksheets("Sheet2")
Set shtE = Worksheets("Sheet3")
' Determine rows and cols to test
With shtA.Range("one")
nFirstRow = .Cells(1, 1).Row
nLastRow = .Rows.Count + .Cells(1, 1).Row - 1
nFirstCol = .Cells(1, 1).Column
nLastCol = .Columns.Count + .Cells(1, 1).Column - 1
End With
With shtB.Range("two")
nFirstRow = WorksheetFunction.Min(nFirstRow, .Cells(1, 1).Row)
nLastRow = WorksheetFunction.Max(nLastRow, .Rows.Count + .Cells(1, 1).Row - 1)
nFirstCol = WorksheetFunction.Min(nFirstCol, .Cells(1, 1).Column)
nLastCol = WorksheetFunction.Max(nLastCol, .Columns.Count + .Cells(1, 1).Column - 1)
End With
Set rng = Range(Cells(nFirstRow, nFirstCol), Cells(nLastRow, nLastCol))
' Set up exceptions sheet
shtE.Cells.Clear
shtE.Cells(1, 1) = "Ref."
shtE.Cells(1, 2) = shtA.Name
shtE.Cells(1, 3) = shtB.Name
nErrorOutRow = 2
' Process all cells and compare
Application.ScreenUpdating = False
For Each a In shtA.Range(rng.Address)
Set b = shtB.Range(a.Address)
If a.Value <> b.Value Then
shtE.Cells(nErrorOutRow, 1) = b.Address(0, 0)
'shtE.Cells(nErrorOutRow, 1) = a.Value
shtE.Cells(nErrorOutRow, 2) = a.Value
shtE.Cells(nErrorOutRow, 3) = b.Value
nErrorOutRow = nErrorOutRow + 1
End If
Next a
Application.ScreenUpdating = True
shtE.Activate
' Clean up and terminate
Set shtA = Nothing
Set shtB = Nothing
Set shtE = Nothing
Set rng = Nothing
Set a = Nothing
Set b = Nothing
End Sub