Hello,
I am using the following code that I found on another forum to compare two data sheets. Whenever I run the code though, it comes up with an error message "Run-time Error: 13 Type: Mismatch" for the line that is highlighted and underlined. It will highlight the differences in Excel, but will not show the pop-up message with the number of errors. Also, is there a way to only apply this code to certain cells of data instead of the whole sheet?
Thank you!
Sub RunCompare()
Call compareSheets("Sheet1", "Sheet2")
End Sub
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", vbInformation
ActiveWorkbook.Sheets(shtSheet2).Select
End Sub
I am using the following code that I found on another forum to compare two data sheets. Whenever I run the code though, it comes up with an error message "Run-time Error: 13 Type: Mismatch" for the line that is highlighted and underlined. It will highlight the differences in Excel, but will not show the pop-up message with the number of errors. Also, is there a way to only apply this code to certain cells of data instead of the whole sheet?
Thank you!
Sub RunCompare()
Call compareSheets("Sheet1", "Sheet2")
End Sub
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", vbInformation
ActiveWorkbook.Sheets(shtSheet2).Select
End Sub