带有2个文本框的逻辑语句

时间:2017-08-17 10:12:16

标签: excel vba excel-vba

当我使用2个文本框进行逻辑语句时,我的代码出现问题。

如果Textbox4的值小于textbox6的绝对值,我只想在关闭用户表单之前检查。

If Me.TextBox4.Value <= Abs(Me.TextBox6.Value) Then
 MyInput = MsgBox("Warning. The absolute max or min signal is bigger then Full Scale. Do you want to continue anyway?", vbYesNo)

(当测试代码时,当textbox4.value现在变小时,msgbox不会激活。)

我错过了什么吗?这不是写它的正确方法吗?

感谢您的帮助。

以下是完整的代码:

    Private Sub selectcmd1_Click()

   Dim MyInput
   Dim ws As Worksheet
   Set ws = Worksheets("InputS&T")


    If Me.TextBox4.Value <= Abs(Me.TextBox6.Value) Then
     MyInput = MsgBox("Warning. The absolute max or min signal is bigger then Full Scale. Do you want to continue anyway?", vbYesNo)

        If MyInput = vbYes Then

'find first empty row in database---------------------------------
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, searchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
ws.Cells(iRow, 3).Value = Me.ComboBox1.Value
ws.Cells(iRow, 4).Value = Me.TextBox1.Value
ws.Cells(iRow, 5).Value = Me.TextBox2.Value
ws.Cells(iRow, 6).Value = Me.TextBox3.Value
ws.Cells(iRow, 7).Value = Me.TextBox4.Value
ws.Cells(iRow, 8).Value = Me.TextBox5.Value
ws.Cells(iRow, 9).Value = Me.TextBox6.Value
ws.Cells(iRow, 10).Value = Me.TextBox7.Value
ws.Cells(iRow, 11).Value = Me.TextBox8.Value
Unload Me

    BeginRow = 13
    EndRow = 40
    ChkCol = 3

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        Else
            Cells(RowCnt, ChkCol).EntireRow.Hidden = False
        End If
    Next RowCnt
'-------------------------------------------------------------
Else

End If

Else

iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, searchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
ws.Cells(iRow, 3).Value = Me.ComboBox1.Value
ws.Cells(iRow, 4).Value = Me.TextBox1.Value
ws.Cells(iRow, 5).Value = Me.TextBox2.Value
ws.Cells(iRow, 6).Value = Me.TextBox3.Value
ws.Cells(iRow, 7).Value = Me.TextBox4.Value
ws.Cells(iRow, 8).Value = Me.TextBox5.Value
ws.Cells(iRow, 9).Value = Me.TextBox6.Value
ws.Cells(iRow, 10).Value = Me.TextBox7.Value
ws.Cells(iRow, 11).Value = Me.TextBox8.Value
Unload Me

    BeginRow = 13
    EndRow = 40
    ChkCol = 3

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        Else
            Cells(RowCnt, ChkCol).EntireRow.Hidden = False
        End If
    Next RowCnt

    End If


End Sub

3 个答案:

答案 0 :(得分:1)

您应该将值转换为Double,然后执行检查:

Dim dblVar1 As Double
Dim dblVar2 As Double

dblVar1 = CDbl(Me.TextBox4.Value)
dblVar2 = CDbl(Me.TextBox6.Value)

If dblVar1 <= Abs(dblVar2) Then
    MyInput = MsgBox("Warning. The absolute max or min signal is bigger then Full Scale. Do you want to continue anyway?", vbYesNo)
End If

原因是虽然你可以在VBA中做这样的事情:

If "10" < "6" Then
    '...
End If

它不会给出预期的结果,因为"10" < "6"True,因为它是文本比较。

答案 1 :(得分:1)

使用

If CLng(Me.TextBox1.Value) <= CLng(Abs(Me.TextBox2.Value)) Then

而不是

If Me.TextBox4.Value <= Abs(Me.TextBox6.Value) Then

答案 2 :(得分:0)

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim myinput As Variant

If Me.TextBox4.Value <= Abs(Me.TextBox6.Value) Then
    myinput = MsgBox("Warning. The absolute max or min signal is bigger then Full Scale. Do you want to continue anyway?", vbYesNo)
End If

If myinput <> vbYes Then Cancel = True

End Sub
相关问题