passing a non numeric cell for numeric comparison

O

oercim

Hello, I have a problem with my vba code,, which compares the values of first and second columns. And if value of first column is smaller than the second one, assigns "1" to third column.

However, if one of the cells is not numeric code stops to work and gives error. Unfortunately some of my cells have to be string. In that situation I just want to pass that row. How can I do it? I couldn't manage it. My codes are as below:


For i = 1 To 100
If Sheets("Sheet1").Cells(i, 1) < Sheets("Sheet1").Cells(i, 2) Then
Sheets("Sheet1").Cells(i, 3) = 1
Else
Sheets("Sheet1").Cells(i, 3) = 0
End If
Next


I will be very glad if you help me. Thanks a lot.
 
C

Claus Busch

Hi,

Am Tue, 28 Aug 2012 02:08:19 -0700 (PDT) schrieb oercim:
However, if one of the cells is not numeric code stops to work and gives error. Unfortunately some of my cells have to be string. In that situation I just want to pass that row. How can I do it? I couldn't manage it. My codes are as below:

For i = 1 To 100
If Sheets("Sheet1").Cells(i, 1) < Sheets("Sheet1").Cells(i, 2) Then
Sheets("Sheet1").Cells(i, 3) = 1
Else
Sheets("Sheet1").Cells(i, 3) = 0
End If
Next

try:

For i = 1 To 100
With Sheets("Sheet1")
If IsNumeric(.Cells(i, 1)) And _
IsNumeric(.Cells(i, 2)) Then
.Cells(i, 3) = -(.Cells(i, 1) < .Cells(i, 2))
End If
End With
Next


Regards
Claus Busch
 
O

oercim

28 Ağustos 2012 Salı 12:24:19 UTC+3 tarihinde Claus Busch yazdı:
Hi,



Am Tue, 28 Aug 2012 02:08:19 -0700 (PDT) schrieb oercim:










try:



For i = 1 To 100

With Sheets("Sheet1")

If IsNumeric(.Cells(i, 1)) And _

IsNumeric(.Cells(i, 2)) Then

.Cells(i, 3) = -(.Cells(i, 1) < .Cells(i, 2))

End If

End With

Next





Regards

Claus Busch

--

Win XP PRof SP2 / Vista Ultimate SP2

Office 2003 SP2 /2007 Ultimate SP2

Thank you very much Claus Busch. This was very helpful for me.
 

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