BuckeyeRJB
New Member
- Joined
- Aug 25, 2015
- Messages
- 9
Hey Everyone,
I am having some issues with my VBA code. I have already coded half of it and it's working fine. I need to have the contents of my 1st and 2nd worksheet generate onto the 3rd worksheet. Now here's the catch, I need it to see if there are any differences between the data on sheet1 and sheet2. if there is a difference i need it to be color filled and displayed onto the 3rd sheet with the number difference between the two. For example: Sheet1 in cell 24a has "3" but yet Sheet2 has that same cell as "5". I need that cell in the 3rd sheet to read "+2". If there is no difference, i still need it to show up on the third worksheet but just have the value of the cell be "0". Only numbers will be affected in this scenario. If the data is just general text then it can simply copy paste over to Sheet3. Thanks for the help guys. The code that I have so far will be below.
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 IsDate(mycell) Then
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
mydiffs = mydiffs + 1
Else
mycell.Interior.ColorIndex = 0
End If
Next
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
If IsDate(mycell) Then
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbYellow
mydiffs = mydiffs + 1
Else
mycell.Interior.ColorIndex = 0
End If
End If
Next
'For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
' If IsDate(mycell) Then
' If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
' If mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
'
' End If
' End If
' End If
'Next
'Display a message box to demonstrate the differences
MsgBox mydiffs & " differences found", vbInformation
ActiveWorkbook.Sheets(shtSheet2).Select
End Sub
I am having some issues with my VBA code. I have already coded half of it and it's working fine. I need to have the contents of my 1st and 2nd worksheet generate onto the 3rd worksheet. Now here's the catch, I need it to see if there are any differences between the data on sheet1 and sheet2. if there is a difference i need it to be color filled and displayed onto the 3rd sheet with the number difference between the two. For example: Sheet1 in cell 24a has "3" but yet Sheet2 has that same cell as "5". I need that cell in the 3rd sheet to read "+2". If there is no difference, i still need it to show up on the third worksheet but just have the value of the cell be "0". Only numbers will be affected in this scenario. If the data is just general text then it can simply copy paste over to Sheet3. Thanks for the help guys. The code that I have so far will be below.
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 IsDate(mycell) Then
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
mydiffs = mydiffs + 1
Else
mycell.Interior.ColorIndex = 0
End If
Next
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
If IsDate(mycell) Then
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbYellow
mydiffs = mydiffs + 1
Else
mycell.Interior.ColorIndex = 0
End If
End If
Next
'For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
' If IsDate(mycell) Then
' If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
' If mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
'
' End If
' End If
' End If
'Next
'Display a message box to demonstrate the differences
MsgBox mydiffs & " differences found", vbInformation
ActiveWorkbook.Sheets(shtSheet2).Select
End Sub