Home > Net >  If-statement comparing two values, one of them double digit
If-statement comparing two values, one of them double digit

Time:08-25

I´m having a weird error in my vba program that compares the size of two values from textboxes. The code goes as follows:

If TextBoxValue1.Value < TextBoxValue2.Value Then
    MsgBox "Value 2 is not allowed to be bigger than Value 1!", vbCritical
    Exit Sub
End If

Now this works perfectly fine as long as I´m typing in single digit values. But as soon Value 1 is double digit (e.g. 10) and Value 2 is single digit (e.g. 9), the MsgBox pops up, even though Value 1 is bigger than Value 2. Is there anything I´m missing?

CodePudding user response:

Use VAL() function to make them number values. Try-

If Val(TextBox1) < Val(TextBox2) Then
    MsgBox "Value 2 is not allowed to be bigger than Value 1!", vbCritical
    Exit Sub
End If
  • Related