从工作表中提取数据后,MsgBox会多次出现

时间:2015-07-01 17:13:59

标签: excel vba excel-vba messagebox msgbox

我想获得以下代码的帮助。我对此非常陌生,但我认为这是一个简单的解决方案,我只是无法将其他搜索中的建议改进我的代码。

msgbox在第一遍时工作正常,检查文本框值是否正确但是当我检查工作表中的公式结果是否正确时,我弹出了5个消息框。< / p>

希望这是有道理的,如果您有任何建议请告诉我!

`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

1 个答案:

答案 0 :(得分:1)

使用Application.EnableEvents property暂时禁用触发事件,然后在完成后重新启用它们。

这样的事情:

Application.EnableEvents = False
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
Application.EnableEvents = True