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

我想得到一些帮助下面的代码。 我很新,但我认为这是一个简单的解决scheme,我只是无法将其他search的build议翻新到我的代码。

msgboxes工作正常,第一遍来检查文本框的值是否正确,但是当我检查一个表单的公式结果是否正确时,我得到5个消息框popup。

希望这是有道理的,让我知道,如果你有任何build议!

`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 

使用Application.EnableEvents属性临时禁用事件触发,然后在完成时重新启用事件。

像这样的东西:

 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