compare two cell values on different sheets

S

sagarh

hi I am a newbie to excel vba programming so please forgive if this is
silly question.
I have an excel spreadsheet with two worksheets sheet1 and sheet2.
need to do an if statement that compares two cell values. The cells o
both sheets will already be active/selected.

Thanks for any hel
 
R

Rob van Gelder

Here's a couple of examples:

Sub test()
If Worksheets("sheet1").Range("A1").Value =
Worksheets("sheet2").Range("A1").Value Then
MsgBox "same"
Else
MsgBox "different"
End If
End Sub

Sub test2()
Dim rng1 As Range, rng2 As Range

Set rng1 = Worksheets("sheet1").Range("A1")
Set rng2 = Worksheets("sheet2").Range("A1")

If rng1.Value = rng2.Value Then
MsgBox "same"
Else
MsgBox "different"
End If
End Sub
 
S

sagarh

What if the current selected cell is not A1 - my two sheets have 400
rows each and I am moving down the rows and doing a compare
 
T

Tom Ogilvy

Dim rng1 as Range, i as Long
Dim cell as Range
With worksheet("Sheet1")
set rng1 = .Range(.Cells(1,1),.Cells(1,1).End(xldown))
End With

i = 0
for each cell in rng1

if cell.Value <> worksheets("Sheet2") _
.Range("A1").Offset(i,0).Value Then
' do what - they don't match
else
' do what - they match
End if
i = i + 1
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top