Comparing Floating Point For Greater Than or Equal To

dbmathis

Well-known Member
Joined
Sep 22, 2002
Messages
1,064
Hi All,

I am working with rating scales and I need to be able to check to see if a floating point number is >= another floating point number with VBA. I only need 2 decimal places for my comparisons. I

For example I on my worksheet I have two different cells that have 40 in them. and do >= it returns false.

Code looks like:

Code:
    For rating_scale_range = ws2.Cells(Rows.Count, numeric_value_column).End(xlUp).Row To row_number_rating_scale Step -1
        If ws2.Cells(row_number_ratings, raw_normalized_rating_column) >= ws2.Cells(rating_scale_range, normalized_threshold_value_column) Then
            ' Numeric
            ws2.Cells(row_number_ratings, calculated_manager_numeric_rating_column) = ws2.Cells(rating_scale_range, numeric_value_column)
            ' Normalized
            ws2.Cells(row_number_ratings, calculated_manager_normalized_rating_column) = ws2.Cells(rating_scale_range, normalized_value_column): Exit For
        End If
    Next

Is there a way to do this?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Code:
If ws2.Cells(row_number_ratings, raw_normalized_rating_column) >= ws2.Cells(rating_scale_range, normalized_threshold_value_column) Then
Don't test the floating point numbers directly, round the values to two decimal places and test that instead...

Code:
If Round(ws2.Cells(row_number_ratings, raw_normalized_rating_column), 2) >= Round(ws2.Cells(rating_scale_range, normalized_threshold_value_column), 2) Then
 
Upvote 0
I tried that, along with WorksheetFunction.Round and it didn't make a difference.
In that case, I think one of your "numbers" is actually text while the other is a real number. If I am right, then you are asking if 40="40" which is not true. If I am right, try wrapping your values in a CDbl function call (and still use the Round function as well)...

Code:
If Round(CDbl(ws2.Cells(row_number_ratings, raw_normalized_rating_column)), 2) >= Round(CDbl(ws2.Cells(rating_scale_range, normalized_threshold_value_column)), 2) Then
 
Upvote 0
I can't reproduce it right now so I will keep an eye on this and post back if I see it again.
 
Upvote 0

Forum statistics

Threads
1,224,543
Messages
6,179,429
Members
452,914
Latest member
echoix

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