VBA: IF 0 < 1 returning false

LeonV AW

New Member
Joined
Jun 9, 2015
Messages
3
So here is my problem section of code (I have added the msgbox as a test line) It is within a much larger with statement for the worksheet.
Code:
For Each part In Range("Level").Cells
    If "0" < 1 Then MsgBox part.Value & " " & .Cells(part.Row + 1, part.Column).Value
    If part.value < .Cells(part.Row + 1, part.Column).Value Then _
    .Cells(part.Row, Range("Part_Type").Column) = "A" Else: _
    .Cells(part.Row, Range("Part_Type").Column) = "D"
Next part
so my sheet is a load of parts and the named range level is where in an assembly tree they go. this is suposed to make sure any assembly lines are labeled A and parts are labeled D
for levels over 1 it works fine however if part.value is 0 and the cell below is 1 (usually the top level of the tree) it reads as false.
I can't figure out why. I have watched the part.value expression and did notice that 0 is stored as a variant/string while other numbers are stored as variant/double but when I tested that with the msgbox line it ran fine.

I could throw in an "or part.value =0" to the if statement but would rather search for a less clunky solution first.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
What is in the Range("Level") range?
Are they formulas? Can you post that formula?
 
Upvote 0
K99: that is what I was testing to see if VBA could not compare "0" (the string) to 1 (the intiger).

Jonmo:
the range("level") range is just a list of numbers.
at this point in the code the sheet has no formulas on it, other than creating the named ranges from the column headers, it is as it is exported from the database (a .csv saved as an .xlsx).
 
Upvote 0
Jonmo:
at this point in the code the sheet has no formulas on it,

Does that mean then at some point previously in the code they were formulas?
That have since been copy/pasted as values?

What I'm getting at is that it sounds like your 0's are not really 0's. They're numbers stored as text.
That's why you see it stored as a string instead of a number.

If you had a formula in there that was doing something like
=IF(A<>B, "0",A)
or whatever, that "0" is the source of the 'number stored as text'
Even after paste special / values.
 
Upvote 0
No this is before any formulas are added. The data is (with the exception of named ranges which is the first thing the code does) as raw.

looking at the output it does seem to be held as text. now I need to figure out why excel won't convert 0 to number when it opens the file like it does all the other numbers.

thanks anyway
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top