VBA Excel Thinks Number is Lower Than Another

realniceguy5000

Board Regular
Joined
Aug 19, 2008
Messages
148
Hi, I have this script below:For some reason excel things the upcstring is lower then LowerTH string?

Setup is:

Range("A17").Value is 15.5 Cell Format is Text
Range("K35") is a formula that results in 14.00
LowerTH is Range("35").Value - 5 results are 9.00

When the script reaches the if statment below it should by pass the inputbox because the upcstring is a higher number than the lowerTH number, But instead it goes right to the inputbox which means excel thinks that the upcstring is lower than the LowerTH number?

Code:
If upcstring < LowerTH Then


Maybe I need to change the text string back to a number?

Not sure???

Thank You, Mike

Here is the script...
Code:
Sub Weight()
Stop
 
    If Sheets(2).Range("A1").Value = "Please Enter Weight From Scale" Then
 
        upcstring = Sheets(2).Range("A17").Value
            mycount = Len(upcstring)
        If mycount = "" Or mycount > 5 Then
            MsgBox "Please Enter Weight Again", vbOKOnly, "Data Error"
                Sheets(2).Range("A17").Value = ""
 
        ElseIf Sheets(1).Range("K35").Value > 0 Then
        Stop
 
 
       [COLOR=red]upcstring = Format(upcstring, "0.00")[/COLOR]
[COLOR=red]           myavg = Sheets(1).Range("K35").Value[/COLOR]
[COLOR=red]               myavg = Format(myavg, "0.00")[/COLOR]
[COLOR=red]                   UpperTH = myavg + 5#: UpperTH = Format(UpperTH, "0.00")[/COLOR]
[COLOR=red]                       LowerTH = myavg - 5#: LowerTH = Format(LowerTH, "0.00")[/COLOR]
[COLOR=red]                           'If upcstring '> UpperTH  'Or[/COLOR]
[COLOR=red]                           If upcstring < LowerTH Then[/COLOR]
 
                                NewValue = Application.InputBox("Is This Weight Correct? " & upcstring & " lbs", "Weight Check", upcstring)
                            End If
 
         Stop
 
 
 
        Else
            With Sheets(1)
            lrow = .Cells(21, 5).End(xlUp).Row + 1
            .Cells(lrow, 5).Value = upcstring
            End With
 
            With Sheets(2)
             .Range("A1").Value = "Checking Data..."
            .Range("A1").Interior.ColorIndex = 50
            .Range("A17").Select
            .Range("A17").Value = ""
 
            End With
        End If
    End If
 
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Convert both to a number in the code and run the test:

Code:
If Val(upcstring) < Val(LowerTH) Then

If it works, you know it is a text comparison issue.
 
Upvote 0
Yep. As a text comparison, 15 is less than 9 as it starts with 1. It would be helpful if you declared some variables and just used number types to compare numbers... :)
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

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