Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two entire rows in a sheet

Tags:

excel

vba

I am new to VBA. I have job in my hand to improve performance of VBA code. To improve performance of the code, I have to read entire row and compare it with another row. Is there any way to do this in VBA?

Pseudocode:

sheet1_row1=read row1 from sheet1
sheet2_row1=read row1 from sheet2
if sheet1_row1 = sheet2_row1 then
      print "Row contains same value"
else
      print "Row contains diff value"
end if
like image 525
Vicky Avatar asked Oct 16 '13 05:10

Vicky


People also ask

How do I compare two rows in sheets?

Compare Two Rows in Google Sheets You can also compare two rows using conditional formatting in Google Sheets. Select the data range you want to compare (here, C2:I3), and in the Menu, go to Format > Conditional formatting.


1 Answers

For your specific example, here are two ways...

Case Insensitive:

MsgBox [and(1:1=2:2)]

Case Sensitive:

MsgBox [and(exact(1:1,2:2))]

...

Below are generalized functions to compare any two contiguous ranges.

Case Insensitive:

Public Function RangesEqual(r1 As Range, r2 As Range) As Boolean
    RangesEqual = Evaluate("and(" & r1.Address & "=" & r2.Address & ")")
End Function

Case Sensitive:

Public Function RangesEqual(r1 As Range, r2 As Range) As Boolean
    RangesEqual = Evaluate("and(exact(" & r1.Address & "," & r2.Address & "))")
End Function
like image 133
Excel Hero Avatar answered Sep 20 '22 19:09

Excel Hero