如果捕获了所有数据,则关闭用户窗体
Close UserForm if All Data Captured
假设您有一个 UserForm
和 TextBox1
、TextBox3
、TextBox3
和一个 OK
按钮。
要仅在所有三个 TextBox
都有数据时才允许 UserForm
关闭,我将使用分配给 OK
按钮的以下脚本:
Private Sub CommandButton1_Click()
If Len(TextBox1.Value) >= 1 And _
Len(TextBox2.Value) >= 1 And _
Len(TextBox3.Value) >= 1 Then
Me.Hide
Else
MsgBox "Please Complete All Fields!"
End If
End Sub
除了 If
声明之外,还有其他方法吗?
您可以使用下面的代码
Private Sub CommandButton1_Click()
If Trim(TextBox1.Value & vbNullString) = vbNullString And _
Trim(TextBox2.Value & vbNullString) = vbNullString And _
Trim(TextBox3.Value & vbNullString) = vbNullString Then
Me.Hide
Else
MsgBox "Please Complete All Fields!"
End If
End Sub
我从这个问题中得到了答案
VBA to verify if text exists in a textbox, then check if date is in the correct format
正如我在评论中所说,这是一种不错的方法。但我会 post 这只是为了让你有另一种方式的例子。这将允许您在设置文本框时评估输入的内容。
Option Explicit
Dim bBox1Value As Boolean
Dim bBox2Value As Boolean
Dim bBox3Value As Boolean
Private Sub TextBox1_Change()
If Trim(TextBox1.Text) <> "" Then
bBox1Value = True
End If
End Sub
Private Sub TextBox2_Change()
If Trim(TextBox2.Text) <> "" Then
bBox2Value = True
End If
End Sub
Private Sub TextBox3_Change()
If Trim(TextBox3.Text) <> "" Then
bBox3Value = True
End If
End Sub
Private Sub CommandButton1_Click()
If bBox1Value = True And bBox2Value = True And bBox3Value = True Then
Me.Hide
Else
MsgBox "Please Complete All Fields!"
End If
End Sub
您可以使用循环:
Private Sub CommandButton1_Click()
Dim n as long
For n = 1 to 3
If Len(Trim(Me.Controls("TextBox" & n).Value)) = 0 Then
MsgBox "Please Complete All Fields!"
Exit Sub
End If
Next n
Me.Hide
End Sub
在错误发生之前指导用户
在进行了无效操作后通知用户更好的做法是防止用户首先执行该无效操作[1]。一种方法是使用 Textbox_AfterUpdate
事件调用共享验证例程,该例程控制 OK 按钮的 Enabled
属性,还控制状态标签的显示。结果是一个信息量更大的界面,只允许有效的操作,从而限制了 msgbox 弹出窗口的滋扰。这是一些示例代码和屏幕截图。
Private Sub TextBox1_AfterUpdate()
RunValidation
End Sub
Private Sub TextBox2_AfterUpdate()
RunValidation
End Sub
Private Sub TextBox3_AfterUpdate()
RunValidation
End Sub
Private Sub RunValidation()
If Len(TextBox1.Value) = 0 Or Len(TextBox2.Value) = 0 Or Len(TextBox3.Value) = 0 Then
CommandButton1.Enabled = False
Label1.Visible = True
Else
CommandButton1.Enabled = True
Label1.Visible = False
End If
End Sub
Private Sub CommandButton1_Click()
Me.Hide
End Sub
If
声明
就 If
语句而言,有很多方法可以完成,但我认为除了直接评估 TextBox.Value
之外的任何其他方法都会导致不必要的管道和代码复杂性,所以我认为除了 OP 中的 If
声明之外,很难为任何事情争论。话虽这么说,这个特定的 If
语句可以通过利用其数字性质来稍微压缩,这允许
Len(TextBox1.Value) = 0 Or Len(TextBox2.Value) = 0 Or Len(TextBox3.Value) = 0
将替换为
Len(TextBox1.Value) * Len(TextBox2.Value) * Len(TextBox3.Value) = 0
虽然这不会给您带来太多好处,而且可以说是代码的可读性较差,但它确实允许使用压缩的一行,尤其是在文本框被重命名的情况下...
If Len(TB1.Value) * Len(TB2.Value) * Len(TB3.Value) = 0 Then
.Value 与 .Text
最后,在这种情况下,我认为应该使用.Value
而不是.Text
。 .Text
更适合在输入时验证文本框条目,但在这种情况下,您要验证文本框的保存数据,这是您从 .Value
.[=29= 获得的数据]
更多用户反馈 - 着色
我差点忘了,我想包括这个如何包含更多用户反馈的例子。在提供有用的反馈和过多的反馈之间取得平衡。如果整体表单很复杂,或者目标用户有偏好,但关键字段的颜色指示通常是有益的,则尤其如此。许多应用程序可能首先显示没有颜色的表单,然后在用户遇到问题时将其着色。
Private InvalidColor
Private ValidColor
Private Sub UserForm_Initialize()
InvalidColor = RGB(255, 180, 180)
ValidColor = RGB(180, 255, 180)
TextBox1.BackColor = InvalidColor
TextBox2.BackColor = InvalidColor
TextBox3.BackColor = InvalidColor
End Sub
Private Sub TextBox1_AfterUpdate()
RunValidation Me.ActiveControl
End Sub
Private Sub TextBox2_AfterUpdate()
RunValidation Me.ActiveControl
End Sub
Private Sub TextBox3_AfterUpdate()
RunValidation Me.ActiveControl
End Sub
Private Sub RunValidation(ByRef tb As MSForms.TextBox)
If Len(tb.Value) > 0 Then
tb.BackColor = ValidColor
Else
tb.BackColor = InvalidColor
End If
If Len(TextBox1.Value) * Len(TextBox2.Value) * Len(TextBox3.Value) = 0 Then
CommandButton1.Enabled = False
Label1.Visible = True
Else
CommandButton1.Enabled = True
Label1.Visible = False
End If
End Sub
Private Sub CommandButton1_Click()
Me.Hide
End Sub
假设您有一个 UserForm
和 TextBox1
、TextBox3
、TextBox3
和一个 OK
按钮。
要仅在所有三个 TextBox
都有数据时才允许 UserForm
关闭,我将使用分配给 OK
按钮的以下脚本:
Private Sub CommandButton1_Click()
If Len(TextBox1.Value) >= 1 And _
Len(TextBox2.Value) >= 1 And _
Len(TextBox3.Value) >= 1 Then
Me.Hide
Else
MsgBox "Please Complete All Fields!"
End If
End Sub
除了 If
声明之外,还有其他方法吗?
您可以使用下面的代码
Private Sub CommandButton1_Click()
If Trim(TextBox1.Value & vbNullString) = vbNullString And _
Trim(TextBox2.Value & vbNullString) = vbNullString And _
Trim(TextBox3.Value & vbNullString) = vbNullString Then
Me.Hide
Else
MsgBox "Please Complete All Fields!"
End If
End Sub
我从这个问题中得到了答案 VBA to verify if text exists in a textbox, then check if date is in the correct format
正如我在评论中所说,这是一种不错的方法。但我会 post 这只是为了让你有另一种方式的例子。这将允许您在设置文本框时评估输入的内容。
Option Explicit
Dim bBox1Value As Boolean
Dim bBox2Value As Boolean
Dim bBox3Value As Boolean
Private Sub TextBox1_Change()
If Trim(TextBox1.Text) <> "" Then
bBox1Value = True
End If
End Sub
Private Sub TextBox2_Change()
If Trim(TextBox2.Text) <> "" Then
bBox2Value = True
End If
End Sub
Private Sub TextBox3_Change()
If Trim(TextBox3.Text) <> "" Then
bBox3Value = True
End If
End Sub
Private Sub CommandButton1_Click()
If bBox1Value = True And bBox2Value = True And bBox3Value = True Then
Me.Hide
Else
MsgBox "Please Complete All Fields!"
End If
End Sub
您可以使用循环:
Private Sub CommandButton1_Click()
Dim n as long
For n = 1 to 3
If Len(Trim(Me.Controls("TextBox" & n).Value)) = 0 Then
MsgBox "Please Complete All Fields!"
Exit Sub
End If
Next n
Me.Hide
End Sub
在错误发生之前指导用户
在进行了无效操作后通知用户更好的做法是防止用户首先执行该无效操作[1]。一种方法是使用 Textbox_AfterUpdate
事件调用共享验证例程,该例程控制 OK 按钮的 Enabled
属性,还控制状态标签的显示。结果是一个信息量更大的界面,只允许有效的操作,从而限制了 msgbox 弹出窗口的滋扰。这是一些示例代码和屏幕截图。
Private Sub TextBox1_AfterUpdate()
RunValidation
End Sub
Private Sub TextBox2_AfterUpdate()
RunValidation
End Sub
Private Sub TextBox3_AfterUpdate()
RunValidation
End Sub
Private Sub RunValidation()
If Len(TextBox1.Value) = 0 Or Len(TextBox2.Value) = 0 Or Len(TextBox3.Value) = 0 Then
CommandButton1.Enabled = False
Label1.Visible = True
Else
CommandButton1.Enabled = True
Label1.Visible = False
End If
End Sub
Private Sub CommandButton1_Click()
Me.Hide
End Sub
If
声明
就 If
语句而言,有很多方法可以完成,但我认为除了直接评估 TextBox.Value
之外的任何其他方法都会导致不必要的管道和代码复杂性,所以我认为除了 OP 中的 If
声明之外,很难为任何事情争论。话虽这么说,这个特定的 If
语句可以通过利用其数字性质来稍微压缩,这允许
Len(TextBox1.Value) = 0 Or Len(TextBox2.Value) = 0 Or Len(TextBox3.Value) = 0
将替换为
Len(TextBox1.Value) * Len(TextBox2.Value) * Len(TextBox3.Value) = 0
虽然这不会给您带来太多好处,而且可以说是代码的可读性较差,但它确实允许使用压缩的一行,尤其是在文本框被重命名的情况下...
If Len(TB1.Value) * Len(TB2.Value) * Len(TB3.Value) = 0 Then
.Value 与 .Text
最后,在这种情况下,我认为应该使用.Value
而不是.Text
。 .Text
更适合在输入时验证文本框条目,但在这种情况下,您要验证文本框的保存数据,这是您从 .Value
.[=29= 获得的数据]
更多用户反馈 - 着色
我差点忘了,我想包括这个如何包含更多用户反馈的例子。在提供有用的反馈和过多的反馈之间取得平衡。如果整体表单很复杂,或者目标用户有偏好,但关键字段的颜色指示通常是有益的,则尤其如此。许多应用程序可能首先显示没有颜色的表单,然后在用户遇到问题时将其着色。
Private InvalidColor
Private ValidColor
Private Sub UserForm_Initialize()
InvalidColor = RGB(255, 180, 180)
ValidColor = RGB(180, 255, 180)
TextBox1.BackColor = InvalidColor
TextBox2.BackColor = InvalidColor
TextBox3.BackColor = InvalidColor
End Sub
Private Sub TextBox1_AfterUpdate()
RunValidation Me.ActiveControl
End Sub
Private Sub TextBox2_AfterUpdate()
RunValidation Me.ActiveControl
End Sub
Private Sub TextBox3_AfterUpdate()
RunValidation Me.ActiveControl
End Sub
Private Sub RunValidation(ByRef tb As MSForms.TextBox)
If Len(tb.Value) > 0 Then
tb.BackColor = ValidColor
Else
tb.BackColor = InvalidColor
End If
If Len(TextBox1.Value) * Len(TextBox2.Value) * Len(TextBox3.Value) = 0 Then
CommandButton1.Enabled = False
Label1.Visible = True
Else
CommandButton1.Enabled = True
Label1.Visible = False
End If
End Sub
Private Sub CommandButton1_Click()
Me.Hide
End Sub