Sanoop Sanu
Sanoop Sanu

Reputation: 3

Error while comparing msgbox with textbox in vba

I am new to VBA. I have created a program in VBA that compares a msgbox value with a textbox value, but the result is not right. I have copied the code below. What have I done wrong on this? Please help me.

Private Sub CommandButton1_Click()

    Dim num As String
    num = Application.InputBox("enter num")
    If TextBox1.Value * num > TextBox2.Value Then
        MsgBox "textbox1 is higher"
    Else
        MsgBox "textbox2 is higher"    
    End If

End Sub

Upvotes: 0

Views: 127

Answers (3)

user3598756
user3598756

Reputation: 29421

You need an input validation before processing it

like follows

Option Explicit

Private Sub CommandButton1_Click()
    Dim num As Long, tb1Val As Long, tb2Val As Long
    Const DEFNUM As Long = 1 '<--| define a default value for 'num'

    If Not ValidateInput(tb1Val, tb2Val) Then Exit Sub '<--| exit if textboxes have improper input

    num = Application.InputBox("enter num", , DEFNUM, Type:=1)  '<-_| 'Type:=1' forces a number input
    With Me
        If tb1Val * num > tb2Val.Value Then
            MsgBox "textbox1 is higher"
        Else
            MsgBox "textbox2 is higher"
        End If
    End With
End Sub

Function ValidateInput(tb1Val As Long, tb2Val As Long) As Boolean
    With Me
        If IsNumber(.TextBox1) And IsNumber(.TextBox2) Then
            tb1Val = CLng(.TextBox1.Value)
            tb2Val = CLng(.TextBox2.Value)
            ValidateInput = True
        Else
            MsgBox "Improper textbox input, try again", vbCritical
        End If
    End With
End Function

as you can see:

  • demanded to Function ValidateInput() the validation of relevant userfom input

    you may want to change it to suit your actual needs

  • used Application.InputBox() function instead of VBA.InputBox() to exploit its Type parameter and force the input to a number

I assumed you need Long numbers, should not this be the case just change all Long occurrences with the needed data type (Double, Integer,...) and CLng() with corresponding Type conversion function (CDbl(), CInt(), ...

Upvotes: 1

Arun Thomas
Arun Thomas

Reputation: 845

What you had to do was just use the Val() function when getting the TextBox values. which means you had to use Val(TextBox1.Value) instead of TextBox1.Value

Private Sub CommandButton1_Click()

Dim num As String
num = Application.InputBox("enter num")
If Val(TextBox1.Value) * num > Val(TextBox2.Value) Then
    MsgBox "textbox1 is higher"
Else
    MsgBox "textbox2 is higher"

End If

End Sub

Upvotes: 0

Shai Rado
Shai Rado

Reputation: 33672

You Need to make sure all values you are getting from the InpoutBox and TextBox are numbers (in my code converted to Long , just to be on the safe side):

Private Sub CommandButton1_Click()

Dim num As Long

' convert the number received from the InputBox to a number (type Long)
num = CLng(Application.InputBox("enter num"))

If CLng(TextBox1.Value) * num > CLng(TextBox2.Value) Then
    MsgBox "textbox1 is higher"
Else
    MsgBox "textbox2 is higher"
End If

End Sub

Upvotes: 0

Related Questions