【发布时间】:2015-07-01 17:13:58
【问题描述】:
我想就以下代码获得一些帮助。我对此很陌生,但我认为这是一个简单的解决方案,我只是无法将其他搜索中的建议改进到我的代码中。
msgboxes 在第一次检查文本框值是否正确时工作正常,但是当我检查工作表中的公式结果是否正确时,我会弹出 5 个消息框。
希望这是有道理的,如果您有任何建议,请告诉我!
`Private Sub SpeedCommand_Click()
Dim ctl As Control
If TextBox1AM180.Value > 12000 And TextBox1AM180.Value <> "" Then
MsgBox "Rate Value is out of range for this boom. Ensure rate value is less than 12,000 lbs./acre", vbExclamation, "Main Bin Application Rate"
Me.TextBox1AM180.SetFocus
Exit Sub
End If
If (TextBox2AM180.Value > 120 Or TextBox2AM180.Value < 20) And TextBox2AM180.Value <> "" Then
MsgBox "Density Value is out of range. Ensure density value is between 20 and 120 lbs./cu ft.", vbExclamation, "Main Bin Density"
Me.TextBox2AM180.SetFocus
Exit Sub
End If
If TextBox3AM180.Value > 12000 And TextBox3AM180.Value <> "" Then
MsgBox "Rate Value is out of range for this boom. Ensure rate value is less than 12,000 lbs./acre", vbExclamation, "Granular Bin Application Rate"
Me.TextBox3AM180.SetFocus
Exit Sub
End If
If (TextBox4AM180.Value > 120 Or TextBox4AM180.Value < 20) And TextBox4AM180.Value <> "" Then
MsgBox "Density Value is out of range. Ensure density value is between 20 and 120 lbs./cu ft.", vbExclamation, "Granular Bin Density"
Me.TextBox4AM180.SetFocus
Exit Sub
End If
' Write data to worksheet
With Range("B4")
.Offset(0, 0).Value = Me.TextBox1AM180.Value
.Offset(1, 0).Value = Me.TextBox2AM180.Value
.Offset(5, 0).Value = Me.TextBox3AM180.Value
.Offset(6, 0).Value = Me.TextBox4AM180.Value
End With
If Range("MaxSpeed1").Value > 30 Then
MsgBox "Based upon rate and density, speed is restricted by machine top end application speed."
Exit Sub
End If
If Range("MaxSpeed2").Value > 30 Then
MsgBox "Based upon rate and density, speed is restricted by machine top end application speed."
Exit Sub
End If
' Hide the form
frmAirmax.Hide
【问题讨论】:
-
这些
TextBox1AM180具有Value属性的东西是什么?你确定这是 VB.NET 吗?事件签名看起来像 VB6。无论如何,.Value 似乎不太可能同时是数字 (...>12000) 和字符串 (...<> "") -
NB 我认为你在比较字符串和整数 TextBox1AM180.Value > 12000 和 TextBox1AM180.Value ""
-
正如@Plutonix 指出的那样,这似乎不是VB.NET。字符串与整数的上下文和比较让我相信这是一个 Excel VBA 宏。 Excel 单元格值可以包含字符串/整数/等。并且像@Kate 那样使用这个属性是有效的,但不是最佳实践;通常最好将 .Text 用于字符串,因为空单元格的值为 0 但文本为“”。无论如何,我没有看到任何导致此代码中出现额外消息框的内容。您是否还有一个显示一个的 worksheet_change 子程序?如果是这样,您在
With Range("B4")块中触发它 4 次。 -
您应该只看到一次,除非您有其他事件会触发此代码或其他代码。您还有其他我们看不到的活动吗?喜欢
Worksheet_Change?如果没有,如果在With Range...下的行添加断点并使用 F8 单步执行会怎样?在到达Exit Sub之前,您到达MsgBox代码多少次?单步执行时代码的执行是否会发生意外? -
不相关,但我会将验证代码移至文本框事件。
标签: vba excel messagebox msgbox