PDA

View Full Version : Type Mismatch error



Tuneman12
08-25-2009, 05:47 AM
I have some code that is trying to compare two cells from two different sheets and when it gets to one cell saying "#N/A" and the other containing a number i get a error.

The code looks something like this:


If worksheets("1").cells(x,i) = worksheets("3").cells(x,i)



Basically I want to test if what is displayed in the cells are different.

p45cal
08-25-2009, 11:17 AM
it depends what you want to do when cells with an error in are encountered.
This code will only do a comparison at all if there's not an error in either of the cells:aaa = Worksheets("1").Cells(x, i)
bbb = Worksheets("3").Cells(x, i)
If Not IsError(aaa) And Not IsError(bbb) Then
'code to run if neither cell contain an error
If aaa = bbb Then
'code to run if they're the same
Else
'code to run if they're different
End If
End If

mikerickson
08-25-2009, 03:43 PM
One way would be to use
If CStr(Worksheets("1").Cells(x,i).Value) = CStr(Worksheets("3").Cells(x,i).Value) Then
If you are sure that the cells are formatted the same, you could use
If Worksheets("1").Cells(x,i).Text = Worksheets("3").Cells(x,i).Text Then