phonesdontfly
New Member
- Joined
- Mar 2, 2016
- Messages
- 16
I have this code (adapted from this forum) that compares sheet 1 and sheet 2, and identifies the differences:
My question --> I am adapting this to be able to be used for any two sheets- not just sheet 1 and 2. I have a userform + combo boxes that display the sheet name options from any given workbook, and then it stores the sheet names as variables. How do I adapt the above code to use the variable values in place of sheet1 and sheet2? My variable names are A and B.
The closest I have come is (sht & A.value) and it doesn't work. Thanks!
Code:
Sub compareSheets(shtSheet1 As String, shtSheet2 As String)
Dim mycell As Range
Dim mydiffs As Integer
'For each cell in sheet2 that is not the same in Sheet1, color it yellow
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.row, mycell.Column).Value Then
mycell.Interior.Color = vbYellow
mydiffs = mydiffs + 1
End If
Next
'Display a message box to demonstrate the differences
MsgBox mydiffs & " differences found (highlighted in yellow)", vbInformation
ActiveWorkbook.Sheets(shtSheet2).Select
end sub
My question --> I am adapting this to be able to be used for any two sheets- not just sheet 1 and 2. I have a userform + combo boxes that display the sheet name options from any given workbook, and then it stores the sheet names as variables. How do I adapt the above code to use the variable values in place of sheet1 and sheet2? My variable names are A and B.
The closest I have come is (sht & A.value) and it doesn't work. Thanks!
Last edited: