Forum Discussion
marshalltj67
Mar 02, 2024Brass Contributor
Data Sheet Compare Macro
Good Afternoon All, I am very new to Macros and VBA and I am currently writing my first Macro to compare two data sheets with a specific format. I currently have two sheets with data outputted as...
NikolinoDE
Mar 02, 2024Platinum Contributor
Here is a code that is untested, maybe it will help you.
Sub CompareTwoTables()
Dim tbl1 As ListObject, tbl2 As ListObject
Dim rngCell As Range
' Set references to the tables
Set tbl1 = Worksheets("Sheet1").ListObjects("Table1")
Set tbl2 = Worksheets("Sheet2").ListObjects("Table2")
' Loop through each cell in the first table
For Each rngCell In tbl1.DataBodyRange
' Compare cell values between tables
If rngCell.Value <> tbl2.DataBodyRange.Cells(rngCell.Row, rngCell.Column).Value Then
' Highlight the entire row in yellow
tbl1.ListRows(rngCell.Row - tbl1.HeaderRowRange.Row + 1).Range.Interior.Color = vbYellow
tbl2.ListRows(rngCell.Row - tbl1.HeaderRowRange.Row + 1).Range.Interior.Color = vbYellow
' Change font color to red
rngCell.Font.Color = vbRed
tbl2.DataBodyRange.Cells(rngCell.Row, rngCell.Column).Font.Color = vbRed
End If
Next rngCell
End Sub
For security reasons, I don't open any external files at the moment.
If it is not what you have in mind, please provide more details about what you would like to achieve, step by step if possible, so that I can understand the translation
.