Comparing numbers stored as text

emirpk

New Member
Joined
Mar 21, 2014
Messages
15
Hi,

I have a bunch of numeric values I want to compare but some are marked as numbers stored as text and some are not. This causes some problems when I use the logical operators for comparison, for example 0.10 < 0.08 would return true. What is the best way to get around this? Should I convert the numbers stored as text to numbers? Or convert all the values to text (not sure if the logical operators work as expected when applied to numbers stored as text)? I'm looking for the safest way because I do not want to mess with my original data too much.

Thanks.
 
Hi

I don't know your data and so I don't know the problem

One thing that could happen is that A1 and B1 are not numbers, but text values.

In that case, when they are converted to number values they may lose the last digits.

For ex.

Code:
Sub Test()
Dim a1 As String, b1 As String
Dim v1, v2

a1 = "1234567890123456789"
b1 = "1234567890123456788"

MsgBox a1 = b1
MsgBox Val(a1) = Val(b1)
End Sub

This is just a guess, of course, I don't know your data.
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi

I don't know your data and so I don't know the problem

One thing that could happen is that A1 and B1 are not numbers, but text values.

In that case, when they are converted to number values they may lose the last digits.

For ex.

Code:
Sub Test()
Dim a1 As String, b1 As String
Dim v1, v2

a1 = "1234567890123456789"
b1 = "1234567890123456788"

MsgBox a1 = b1
[COLOR=#FF0000][B]MsgBox Val(a1) = Val(b1)[/B][/COLOR]
End Sub

This is just a guess, of course, I don't know your data.
To preserve the value out to 28 digits, use CDec instead of Val...

MsgBox CDec(a1) = CDec(b1)
 
Upvote 0
Well, the truth is that if they are strigs why do you convert them to anything? If you want to compare their values why not compare them directly

Code:
MsgBox a1 = b1

Like before this is just guessing, of course, I don't know your data and I don't know what you need.
 
Upvote 0
Well, the truth is that if they are strigs why do you convert them to anything? If you want to compare their values why not compare them directly
Perhaps they could be of different lengths?
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,047
Members
449,064
Latest member
scottdog129

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