简体   繁体   English

在VBA中将msgbox与文本框进行比较时出错

[英]Error while comparing msgbox with textbox in vba

I am new to VBA. 我是VBA的新手。 I have created a program in VBA that compares a msgbox value with a textbox value, but the result is not right. 我在VBA中创建了一个程序,该程序将msgbox值与文本框值进行比较,但结果不正确。 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

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 要求Function ValidateInput()验证相关的userfom输入

    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 使用Application.InputBox()函数而不是VBA.InputBox()来利用其Type参数并将输入强制为数字

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() , ... 我假设你需要Long的数字,不应该是这种情况只是更改所有Long的出现与所需要的数据类型( DoubleInteger ,...)和CLng()与相应的类型转换功能CDbl() CInt() ...

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): 您需要确保从InpoutBoxTextBox获得的所有值都是数字(为了安全起见,在我的代码中转换为Long ):

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

What you had to do was just use the Val() function when getting the TextBox values. 您要做的只是在获取TextBox值时使用Val()函数。 which means you had to use Val(TextBox1.Value) instead of TextBox1.Value 这意味着您必须使用Val(TextBox1.Value)而不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM