逻辑语句与2个文本框

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

我只想在closures我的用户表单之前检查,如果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) 

(当testing代码时,当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 

您应该将值转换为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因为它是一个文本比较。

使用

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

代替

 If Me.TextBox4.Value <= Abs(Me.TextBox6.Value) Then 
 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