所以我有一段时间的用户表单,并且使用下面的代码使一些字段成为必填字段
If Me.DateBox.Value = "" Then
DateBox.BackColor = vbRed
MsgBox "Date Field Can Not be Empty"
End If
现在的问题是,尽管这确实将空白字段标记为红色并显示一条消息,但它只允许用户在执行上述代码后处理导出的数据以达到excel。但是我要它做的是不让用户在不填充空白文本框的情况下传输信息。
这是完整的代码。
Private Sub CommandButton1_Click()
Dim addlist As Range
Dim x As Integer
Dim wf As WorksheetFunction
Dim y As Integer
Dim addlist2 As Range
Dim lNextRow As Long
Dim ans As Long
Set wf = Application.WorksheetFunction
Set addlist = Sheet1.Cells(Rows.Count, 7).End(xlUp).Offset(1, 0)
Set addlist2 = Sheet1.Cells(Rows.Count, 7).End(xlUp).Offset(1, 1)
For x = 1 To WortSelector.ListCount - 1
If Me.WortSelector.Selected(x) Then
addlist = Me.WortSelector.List(x)
Set addlist = addlist.Offset(1, 0)
addlist2 = Me.WortSelector.List(x)
Set addlist2 = addlist2.Offset(1, 0)
End If
Next x
lNextRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
Cells(lNextRow, 2) = DateBox.Text
Cells(lNextRow, 3) = PrBox.Text
Cells(lNextRow, 4) = BrewBox.Text
Cells(lNextRow + 1, 9) = RmBox1.Text
Cells(lNextRow, 10) = OgBox.Text
Cells(lNextRow + 2, 9) = RmBox2.Text
Cells(lNextRow + 3, 9) = RmBox3.Text
Cells(lNextRow + 4, 9) = RmBox4.Text
Cells(lNextRow + 5, 9) = RmBox5.Text
Cells(lNextRow + 6, 9) = RmBox6.Text
Cells(lNextRow + 7, 9) = RmBox7.Text
Cells(lNextRow + 8, 9) = RmBox8.Text
Cells(lNextRow + 9, 9) = RmBox9.Text
Cells(lNextRow + 10, 9) = RmBox10.Text
Cells(lNextRow + 11, 9) = RmBox11.Text
Cells(lNextRow + 12, 9) = RmBox12.Text
Cells(lNextRow + 1, 8) = rm1
Cells(lNextRow + 2, 8) = rm2
Cells(lNextRow + 3, 8) = rm3
Cells(lNextRow + 4, 8) = rm4
Cells(lNextRow + 5, 8) = rm5
Cells(lNextRow + 6, 8) = rm6
Cells(lNextRow + 7, 8) = rm7
Cells(lNextRow + 8, 8) = rm8
Cells(lNextRow + 9, 8) = rm9
Cells(lNextRow + 10, 8) = rm10
Cells(lNextRow + 11, 8) = rm11
Cells(lNextRow + 12, 8) = rm12
Cells(lNextRow, 9) = VolumeBox.Text
Do
Set addlist = Sheet1.Cells(Rows.Count, 7).End(xlUp).Offset(1, 0)
For x = 1 To WortSelector.ListCount - 1
If Me.WortSelector.Selected(x) Then
addlist = Me.WortSelector.List(x)
End If
Next x
lNextRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
Cells(lNextRow, 2) = DateBox.Text
Cells(lNextRow, 3) = PrBox.Text
Cells(lNextRow, 4) = BrewBox.Text
Loop Until Sheet1.Cells(lNextRow + 1, 8).Value = ""
If Me.DateBox.Value = "" Then
DateBox.BackColor = vbRed
MsgBox "Date Field Can Not be Empty"
End If
If Me.PrBox.Value = "" Then
PrBox.BackColor = vbRed
MsgBox "PR No. Field Can Not be Empty"
End If
If Me.BrewBox.Value = "" Then
BrewBox.BackColor = vbRed
MsgBox "Brew Number Field Can Not be Empty"
End If
End Sub
谢谢!
答案 0 :(得分:0)
添加Exit Sub
,如下所示。它进一步停止了代码执行。
If Me.DateBox.Value = "" Then
DateBox.BackColor = vbRed
MsgBox "Date Field Can Not be Empty"
Exit sub
End If
Private Sub CommandButton1_Click()
Dim addlist As Range
Dim x As Integer
Dim wf As WorksheetFunction
Dim y As Integer
Dim addlist2 As Range
Dim lNextRow As Long
Dim ans As Long
'Validation start
If Me.DateBox.Value = "" Then
DateBox.BackColor = vbRed
MsgBox "Date Field Can Not be Empty"
Exit Sub
End If
If Me.PrBox.Value = "" Then
PrBox.BackColor = vbRed
MsgBox "PR No. Field Can Not be Empty"
Exit Sub
End If
If Me.BrewBox.Value = "" Then
BrewBox.BackColor = vbRed
MsgBox "Brew Number Field Can Not be Empty"
Exit Sub
End If
'Validation end
Set wf = Application.WorksheetFunction
Set addlist = Sheet1.Cells(Rows.Count, 7).End(xlUp).Offset(1, 0)
Set addlist2 = Sheet1.Cells(Rows.Count, 7).End(xlUp).Offset(1, 1)
For x = 1 To WortSelector.ListCount - 1
If Me.WortSelector.Selected(x) Then
addlist = Me.WortSelector.List(x)
Set addlist = addlist.Offset(1, 0)
addlist2 = Me.WortSelector.List(x)
Set addlist2 = addlist2.Offset(1, 0)
End If
Next x
lNextRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
Cells(lNextRow, 2) = DateBox.Text
Cells(lNextRow, 3) = PrBox.Text
Cells(lNextRow, 4) = BrewBox.Text
Cells(lNextRow + 1, 9) = RmBox1.Text
Cells(lNextRow, 10) = OgBox.Text
Cells(lNextRow + 2, 9) = RmBox2.Text
Cells(lNextRow + 3, 9) = RmBox3.Text
Cells(lNextRow + 4, 9) = RmBox4.Text
Cells(lNextRow + 5, 9) = RmBox5.Text
Cells(lNextRow + 6, 9) = RmBox6.Text
Cells(lNextRow + 7, 9) = RmBox7.Text
Cells(lNextRow + 8, 9) = RmBox8.Text
Cells(lNextRow + 9, 9) = RmBox9.Text
Cells(lNextRow + 10, 9) = RmBox10.Text
Cells(lNextRow + 11, 9) = RmBox11.Text
Cells(lNextRow + 12, 9) = RmBox12.Text
Cells(lNextRow + 1, 8) = rm1
Cells(lNextRow + 2, 8) = rm2
Cells(lNextRow + 3, 8) = rm3
Cells(lNextRow + 4, 8) = rm4
Cells(lNextRow + 5, 8) = rm5
Cells(lNextRow + 6, 8) = rm6
Cells(lNextRow + 7, 8) = rm7
Cells(lNextRow + 8, 8) = rm8
Cells(lNextRow + 9, 8) = rm9
Cells(lNextRow + 10, 8) = rm10
Cells(lNextRow + 11, 8) = rm11
Cells(lNextRow + 12, 8) = rm12
Cells(lNextRow, 9) = VolumeBox.Text
Do
Set addlist = Sheet1.Cells(Rows.Count, 7).End(xlUp).Offset(1, 0)
For x = 1 To WortSelector.ListCount - 1
If Me.WortSelector.Selected(x) Then
addlist = Me.WortSelector.List(x)
End If
Next x
lNextRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
Cells(lNextRow, 2) = DateBox.Text
Cells(lNextRow, 3) = PrBox.Text
Cells(lNextRow, 4) = BrewBox.Text
Loop Until Sheet1.Cells(lNextRow + 1, 8).Value = ""
End Sub