I am trying to compare two excelworkbooks and list the differences in this workbook.
I am getting error "Type Mismatched" in the below line:
If varSheetA <> varSheetB Then
Code:
Sub compareworkb()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Set wbkA = Workbooks.Open(Filename:="C:\Solution - Beginners template .xlsx")
Set wbkB = Workbooks.Open(Filename:="C:\Template_Project Lead - Beginners.xlsx")
For i = 1 To wbkA.Sheets.Count
Set varSheetA = wbkA.Worksheets(wbkA.Sheets(i).Name)
Set varSheetB = wbkB.Worksheets(wbkB.Sheets(i).Name)
strRangeToCheck = "A1:N100"
Debug.Print Now
varSheetA = varSheetA.Range(strRangeToCheck)
varSheetB = varSheetB.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 <> varSheetB Then
wbkB.Sheets(ShName).Cells(iRow, iCol).Interior.Color = vbYellow
ThisWorkbook.Sheets(1).Cells(7 + sh, 2) = "Mismatch Found"
ThisWorkbook.Sheets(1).Cells(7 + sh, 2).Interior.Color = vbYellow
End If
Next
Next
Next i
End Sub
You are looping through every element of the arrays, but then trying to compare the entire arrays each time, which cannot be done with <>. Instead, just add the index of the items you are trying to compare
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
' Note this change vvvvv vvvvvv
If varSheetA(iRow,iCol) <> varSheetB(iRow,iCol) Then
wbkB.Sheets(ShName).Cells(iRow, iCol).Interior.Color = vbYellow
ThisWorkbook.Sheets(1).Cells(7 + sh, 2) = "Mismatch Found"
ThisWorkbook.Sheets(1).Cells(7 + sh, 2).Interior.Color = vbYellow
End If
Next iCol
Next iRow
Edit:
It looks like you've changed your mind about how to loop through the sheets, and forgotten to update the variable names. For instance you are trying to reference the sheet ShName, when you haven't defined ShName, and you are trying to write to row 7+sh when you haven't defined sh! You also hadn't then declared i as a variable before using it to loop.
Also, you are getting a type mismatch error when there is a variable type mismatch in your worksheet! For instance if you have an error in one cell, and an integer in the corresponding cell on the other sheet, you can't compare these things using <>. In the below code, I've included a variable type check, see comments for details. The code also encompasses the previously mentioned corrections:
Sub compareworkb()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Dim i As Long
Dim mismatch As Boolean
Set wbkA = ThisWorkbook
Set wbkB = Workbooks("test2.xlsx")
strRangeToCheck = "A1:N100" ' Define this once outside the loop, as it doesn't change
For i = 1 To wbkA.Sheets.Count
varSheetA = wbkA.Worksheets(i).Range(strRangeToCheck)
varSheetB = wbkB.Worksheets(i).Range(strRangeToCheck)
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
mismatch = False
' Check that cell values are the same variable type
If VarType(varSheetA(iRow, iCol)) = VarType(varSheetB(iRow, iCol)) Then
' If they are the same variable type, we can compare them!
If varSheetA(iRow, iCol) <> varSheetB(iRow, iCol) Then
mismatch = True
End If
Else ' If they are not the same variable type, then it must be a mismatch
mismatch = True
End If
' If mismatch found then note it / colour corresponding cell
If mismatch Then
wbkB.Sheets(i).Cells(iRow, iCol).Interior.Color = vbYellow
ThisWorkbook.Sheets(1).Cells(7 + i, 2) = "Mismatch Found"
ThisWorkbook.Sheets(1).Cells(7 + i, 2).Interior.Color = vbYellow
End If
Next iCol
Next iRow
Next i
End Sub
Related
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.
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
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.
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
I'm trying to compare cells in 2 tabs (master and test) and if there are changes in test then color changes in any kind of color and copy & paste it to master file.
UPDATED:
Here is required code
Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Dim jRow As Long
Dim jCol As Long
strRangeToCheck = "A1:V1000"
' 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 varSheetB(iRow, iCol) = varSheetA(iRow, iCol) Then
' Cells are identical.
' Do nothing.
Else
Sheets("Sheet1").Select
Cells(iRow, iCol).Interior.ColorIndex = 44
Sheets("Sheet2").Select
Cells(iRow, iCol).Interior.ColorIndex = 44
Sheets("Sheet2").Select
Cells(iRow, iCol).Copy
Sheets("Sheet1").Select
Cells(iRow, iCol).PasteSpecial xlValues
Cells(iRow, iCol).PasteSpecial xlFormats
' Cells are different.
' Code goes here for whatever it is you want to do.
End If
Next iCol
Next iRow
MsgBox ("Done")
End Sub
Find the last used cell in one of the worksheets.
dim lr as long, lc as long
lr= application.max(dWS.cells.specialcells(xlCellTypeLastCell).row, _
mWS.cells.specialcells(xlCellTypeLastCell).row)
lc= application.max(dWS.cells.specialcells(xlCellTypeLastCell).Column, _
mWS.cells.specialcells(xlCellTypeLastCell).Column)
For Each c In dWS.Range("A2", dWS.cells(lr, lc))
If Not dWS.Cells(c.Row, c.Column).Value = mWS.Cells(c.Row, c.Column).Value Then
dWS.Cells(c.Row, c.Column).Interior.Color = vbYellow
End If
Next