I would like to change the following code to compare entire rows instead of individual cells. I'm a beginner at vba so please explain in simple terms.
Sub RunCompare()
Call compareSheets("Sheet1", "Sheet2")
End Sub
Sub compareSheets(shtBefore As String, shtAfter As String)
'Compares sheets by cells and highlight difference
Dim MyCell As Range
Dim mydiffs As Integer
For Each MyCell In ActiveWorkbook.Worksheets(shtAfter).UsedRange
If Not MyCell.Value = ActiveWorkbook.Worksheets(shtBefore).Cells(MyCell.Row, MyCell.Column).Value Then
MyCell.Interior.Color = vbYellow
mydiffs = mydiffs + 1
End If
Next
MsgBox mydiffs & " differences found", vbInformation
ActiveWorkbook.Sheets(shtAfter).Select
End Sub
There is no way to compare row. You can improve your current method.
1. Set mydiffs
to long type (vba initial value with long, so no need convert to integer)
2. Add Application.ScreenUpdating = False
to enhance the script performance.
Sub RunCompare()
Call compareSheets("Sheet1", "Sheet2")
End Sub
Sub compareSheets(shtBefore As String, shtAfter As String)
'Compares sheets by cells and highlight difference
Dim MyCell As Range
Dim mydiffs As Long
Application.ScreenUpdating = False
For Each MyCell In ActiveWorkbook.Worksheets(shtAfter).UsedRange
If Not MyCell.Value = ActiveWorkbook.Worksheets(shtBefore).Cells(MyCell.Row, MyCell.Column).Value Then
MyCell.Interior.Color = vbYellow
mydiffs = mydiffs + 1
End If
Next
MsgBox mydiffs & " differences found", vbInformation
ActiveWorkbook.Sheets(shtAfter).Select
Application.ScreenUpdating = True
End Sub
The Range
object is a strange beast in Excel and it can take some getting used to its various characteristics.
The phrase
ActiveWorkbook.Worksheets(shtAfter).UsedRange
delivers a Range
object and when you use the loop
For Each MyCell In ActiveWorkbook.Worksheets(shtAfter).UsedRange
what you are actually doing is implicitly relying on the Cells
property of the Range
object to deliver an object that contains all the cells in that range. Excel's help system (in version Office 2010, at least) also indicates this latter object is a Range
object and I suspect this is a source of confusion amongst beginners, because each of the cells is also a Range
object in its own right (so the Cells
property of a Range
delivers an object which is also a Range
though different from its parent and which has "elements" each of which is a Range
)
The loop above is really a shorthand form of
For Each MyCell In ActiveWorkbook.Worksheets(shtAfter).UsedRange.Cells
The Range
object has many properties, one of which is the Rows
property. The phrase
ActiveWorkbook.Worksheets(shtAfter).UsedRange.Rows
delivers an object that contains the separate rows of your UsedRange
and you can then use a loop such as
For each myRow in ActiveWorkbook.Worksheets(shtAfter).UsedRange.Rows
to look at each row in turn. Here myRow
is also a Range object. Again, perhaps confusingly, the Rows
property also delivers a Range
object which contains "elements", each of which is also a Range
object.
Unfortunately, you cannot implicitly rely on the Cells
property with the myRow
object to loop over the individual cells within each row. So
For each myCell in myRow
doesn't work as you'd hope but by explicitly adding the Cells
property
For each myCell in myRow.Cells
does.
In summary , you can achieve your row by row comparison by using two loops: the first for the rows (based on the Rows
property) and a second, nested inside the first, for the cells within a row (based on the Cells
property).
As an aside , you can do much of what you want without using VBA at all. Array formulas in Excel (the ones that require CTL+SHIFT+ENTER when entered from the formula bar) can compare two arrays. For example, the array formula
{=AND(Sheet1!A1:Z1=Sheet2!A1:Z1)}
tells whether the range A1:Z1 is the same on two different worksheets and there are other array formulas which can be used to count the number of differences between two ranges.
If you want to highlight the differences between cells in two worksheets use conditional formatting. The trick here is to set the conditional formatting using a formula on the first cell and then to copy this formatting to the other cells. So set the conditional formatting for cell Sheet2!A1
using the formula
=Sheet1!A1<>Sheet2!A1
Make sure that the formula uses relative A1
rather than absolute $A$1
cell addresses (editting the formula if necessary) and then Copy
the format (using Paste Special Format
) from cell Sheet2!A1
to the rest of the cells on Sheet2
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.