Compare Two Excel Sheets – Highlight Differences – Macro - excel

Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Set varSheetA = Sheet1
strRangeToCheck = "A1:B65536"
' If you know the data will only be in a smaller range, reduce the size of the ranges above.
Debug.Print Now
varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)
varSheetB = Worksheets("Sheet2").Range(strRangeToCheck) ' or whatever your other sheet is.
Debug.Print Now
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
' Cells are identical.
' Do nothing.
Else
' Cells are different.
' Code goes here for whatever it is you want to do.
End If
Next iCol
Next iRow
End Sub
Please find the above code.
I need to highlight the different cells. I tried as mentioned below comments issue has not resolved. Please any one help me on this!

varSheetA is an array that holds the values of the range, it doesn't know of the cells on the sheet.
(If varSheetA should be the range then you would have to write set varSheetA = ... plus defining the variable as range)
worksheets("Sheet1").cells(iRow, iCol).color = vbRed
should work
BTW: it is much easier for us to help if you post the code itself and not a picture - how else should we "take the code"?

Try it this way.
Sub CheckTwoSheetsAndHighlightDifferences()
Dim mycell As Range
Dim mydiff As Integer
Dim shtSheet1 As Worksheet
Dim shtSheet2 As Worksheet
Set shtSheet1 = Worksheets("Sheet1")
Set shtSheet2 = Worksheets("Sheet2")
For Each mycell In shtSheet2.UsedRange
If Not mycell.Value = shtSheet1.Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
mydiffs = mydiffs + 1
End If
Next
End Sub

What about:
varSheetA(iRow, iCol).Color = RGB(0,255,0)
When you take into account that 255,0,0 is red and 0,255,0 is green, that should be very visible.

Related

How can I write VBA to compare every cell on one sheet to every shell on another sheet?

I am trying to compare each cell on one sheet to each cell on another sheet and then highlight the differences. For example, I want to see if cell a2 on one sheet is the same as cell a2 on another sheet. At one point, I was able to run a comparison, but it was just looking for a match within the whole sheet, not just specific cells, so I was getting false positives. I am obviously missing the piece that makes the cell to cell comparison. New to VBA so here is what I have cobbled together so far:
Option Explicit
Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Dim cell As Variant
strRangeToCheck = "A1:Z5000"
Debug.Print Now
varSheetA = Worksheets("RoboReader").Range(strRangeToCheck)
varSheetB = Worksheets("Uploader").Range(strRangeToCheck)
Debug.Print Now
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
' Cells are identical.
' Do nothing.
Else
Cells.Font.Bold = True
Cells.Font.ColorIndex = 2
Cells.Interior.ColorIndex = 8
Cells.Interior.Pattern = xlSolid
End If
Next iCol
Next iRow
End Sub
Cell by Cell Comparison
When using Cells without a qualifier, it means all (1048576*16384)
cells on the Activesheet.
When you use the equal sign (=) in an If statement, the opposite
is (<>). You can eliminate the equal part since nothing will be
happening then.
A Quick Fix
Option Explicit
Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Dim cell As Variant
'Worksheets("Uploader").Cells.ClearFormats
strRangeToCheck = "A1:Z5000"
Debug.Print Now
varSheetA = Worksheets("RoboReader").Range(strRangeToCheck)
varSheetB = Worksheets("Uploader").Range(strRangeToCheck)
Debug.Print Now
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) <> varSheetB(iRow, iCol) Then
With Worksheets("Uploader").Cells(iRow, iCol)
.Font.Bold = True
.Font.ColorIndex = 2
.Interior.ColorIndex = 8
.Interior.Pattern = xlSolid
End With
End If
Next iCol
Next iRow
End Sub

I want to compare all cells of sheet 1 to sheet 2 and matched values turn yellow

I want to compare all cells of sheet 1 to sheet 2 and matched values turn yellow, following code i used,
Sub match()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
strRangeToCheck = "A1:IV65536"
varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)
varSheetB = Worksheets("Sheet2").Range(strRangeToCheck)
Debug.Print Now
For icell = LBound(varSheetA, 1) To UBound(varSheetA, 1)
If varSheetA(icell) = varSheetB(icell) Then
cell.Interior.Color = vbYellow
`enter code here` End If
Next icell
End Sub
Try
Sub match()
Dim shtA As Worksheet
Dim shtB As Worksheet
Dim strRangeToCheck As String
Dim cA as range, cB as range
strRangeToCheck = "A1:IV65536"
set shtA = Worksheets("Sheet1")
set shtB = Worksheets("Sheet2")
Debug.Print Now
For Each cA IN shtA.Range(strRangeToCheck)
on error resume next
if cA.value <> "" then
Set cB = shtB.Range(strRangeToCheck).find(what:=cA.value, lookin:=xlvalues, lookat:=xlwhole)
If Not cB is Nothing then
cA.Interior.Color = vbYellow
End If
end if
on error goto 0
Next cA
End Sub

Excel-VBA: Loop to highlight cells

The second half of my code (a loop) needs to highlight cells if they are not equal to another worksheet. I am getting a error on the varSheetB line under the "else" section of the loop. I believe I am using the wrong syntax to tell it to highlight those cells.
This should be an easy fix. Can someone please provide the correct syntax for telling it to highlight the cells under the "else" portion of the loop?
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
' Cells are identical.
' Do nothing
Else
' Cells are different.
' Highlight different cells yellow.
varSheetB.Range.(iRow & iCol).Interior.ColorIndex = 36
End If
Next iCol
Next iRow
End Sub
Edit: Adding the full code.
Option Explicit
Sub Compare()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
strRangeToCheck = "A12:G150"
Debug.Print Now
varSheetA = Worksheets("Main").Range(strRangeToCheck)
varSheetB = Worksheets("Discrepancy Compare").Range(strRangeToCheck) ' or whatever your other sheet is.
Debug.Print Now
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
' Cells are identical.
' Do nothing.
Else
' Cells are different.
' Highlight different cells yellow.
varSheetB.Range.(iRow & iCol).Interior.ColorIndex = 36
End If
Next iCol
Next iRow
End Sub
Now Tested
Option Explicit
Sub Compare()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
strRangeToCheck = "A12:G150"
varSheetA = Worksheets("Main").Range(strRangeToCheck)
varSheetB = Worksheets("Discrepancy Compare").Range(strRangeToCheck) ' or whatever your other sheet is.
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) <> varSheetB(iRow, iCol) Then
' Cells are different.
' Highlight different cells yellow.
Worksheets("Discrepancy Compare").Cells(iRow + 11, iCol).Interior.ColorIndex = 36
End If
Next iCol
Next iRow
End Sub
Range is not Range.() it is Range().
But, range will expect a Character string for the column and you are passing a number.
In this instance use Cells() which will allow the use of a number for the column:
varSheetB.Cells(iRow, iCol).Interior.ColorIndex = 36
But you need to ensure the iRow and iCol do not start at 0, depending on your setup and how the arrays were filled you may start at 0.
Also, unless you start loading the array from A1 the column and Rows will be off.

VBA code to color mismatch data

I'd like to know a piece of code that highlight when cell value is different from one sheet 1 to sheet 2.
For example in cell A2(sheet1) has Value asd24 and in cell A2(sheet2) has value asd25 then that particular cell must highlighted with any color either in sheet 1 or sheet 2.
I have already developed the with check the value of the cell:
Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Dim R As Range
strRangeToCheck = "A1:IV65536"
' If you know the data will only be in a smaller range, reduce the size of the ranges above.
Debug.Print Now
varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)
varSheetB = Worksheets("Sheet2").Range(strRangeToCheck) ' or whatever your other sheet is.
Debug.Print Now
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
' Cells are identical.
' Do nothing.
Else
**/*/dont know what to do here***
End If
Next iCol
Next iRow
End Sub

Highlight Duplicates between two sheets of 2 workbook

I am creating a macro that will compare the sheets of two workbook. And I need the row to be highlighted if found duplicate in a sheet of another workbook. So far, I have been search and I found code but I am not sure how I can highlight the cell / entire row if it found a duplicate in another sheet of another workbook. Here is my code below:
Sub CompareWorkbooks()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
strRangeToCheck = "B2:D49"
Set wbkA = ActiveWorkbook
Set varSheetA = wbkA.Worksheets("SAP").Range(strRangeToCheck)
Set wbkB = Workbooks.Open(FileName:="C:\Request Distribution\Reminder 20170302.xls")
Set varSheetB = wbkB.Worksheets("SAP").Range(strRangeToCheck)
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
'What to put to hightlight the cell / entire row
Else
'Some msgbox to display that there are no duplicates between sheets of 2 workbooks
End If
Next
Next
End Sub
Sub CompareWorkbooks()
Dim varSheetA As Range
Dim varSheetB As Range
Dim r As Range
Dim wbkA As Workbook
Dim rFind As Range
Set wbkA = ActiveWorkbook
With wbkA.Worksheets("SAP")
.UsedRange.Interior.ColorIndex = xlNone
Set varSheetA = .Range("B2", .Range("B" & Rows.Count).End(xlUp))
End With
Set wbkB = Workbooks.Open(Filename:="C:\Request Distribution\Reminder 20170302.xls")
Set varSheetB = wbkB.Worksheets("SAP").Range(varSheetA.Address)
For Each r In varSheetA
Set rFind = varSheetB.Find(What:=r, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
r.Interior.Color = RGB(127, 187, 199)
End If
Next r
End Sub

Resources