emihir0
emihir0

Reputation: 1260

True for all values but 1, cannot figure out why (VBA)

My loop goes over a range of cells and compares the values:

For k = 7 To wsPlany.UsedRange.Rows.Count
    If wsPlany.Cells(k, 1) = datu Then
        'code here
    End If
Next k

Basically the cells are cells with numbers, 1, 2, 3 ... and the datuis a number too. It all works but for the value 1 and I cannot figure out why.

Even debug.printing something like: wsPlany.Cells(k, 1), datu and comparing them into next line confirms that if value is 2 or higher, it will return true for that line, if it is 1 exactly, it returns false as if it were comparing with other number (but I see they are the same as they are side by side in debug.print).

Upvotes: 0

Views: 71

Answers (1)

emihir0
emihir0

Reputation: 1260

A simple addition of 1 line where I turn the values to Integers no matter what data type they were before solved the problem.

For k = 7 To wsPlany.UsedRange.Rows.Count
    'turns the values to integers first
    wsPlany.Cells(k, 1).Value = CInt(wsPlany.Cells(k,1))

    If wsPlany.Cells(k, 1) = datu Then
        'code here
    End If
Next k

Upvotes: 2

Related Questions