vba 验证用户窗体以检查空控件,使用循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20370769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:47:54  来源:igfitidea点击:

Validation for Userform to check for empty controls, using loops

excelvbauserform

提问by Gervina

After the user done entering the data. When the user click Submit button. I will check if all the textbox is not empty. The user will enter number of member, if the user enter 1 member I will check if member01 is empty, if the user enter 3 member I will check if member01, member02, member03 is empty It works for If Else but I would like to do it for loop, as it is tedious for me to do it 10 times. I don't know how do I change it from If Else to For Loop.

用户完成数据输入后。当用户点击提交按钮时。我会检查所有文本框是否不为空。用户将输入成员编号,如果用户输入 1 个成员我将检查 member01 是否为空,如果用户输入 3 个成员我将检查 member01、member02、member03 是否为空 它适用于 If Else 但我想做它 for 循环,因为我做 10 次很乏味。我不知道如何将它从 If Else 更改为 For 循环。

 ‘Using If Else
  If txtNoMember.Value = "" Then
        MsgBox "Please enter the Number of Member.", vbExclamation, "Input Data"
        txtNoMember.SetFocus
        Exit Sub
    End If

    If txtNoMember.Value = 1 And txtMember01.Value = "" Then
        MsgBox "Member cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    End If

    If txtNoMember.Value = 2 And txtMember01.Value = "" And txtMember02.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    ElseIf txtNoMember.Value = 2 And txtMember01.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    ElseIf txtNoMember.Value = 2 And txtMember02.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    End If

    If txtNoMember.Value = 3 And txtMember01.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    ElseIf txtNoMember.Value = 3 And txtMember02.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    ElseIf txtNoMember.Value = 3 And txtMember03.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    End If

‘Using Loop
Dim Ctrl As Control
Dim CtrlNum As Long

For Each Ctrl In Me.Controls
    If Ctrl.Name Like "txtMember##" Then
        CtrlNum = CLng(Right$(Ctrl.Name, 2))
        If CtrlNum.Value = "" Then
            MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        End If
    End If
Next

回答by MiVoth

You could do something like this:

你可以这样做:

Private Sub CommandButton1_Click()
If txtNoMember.Value = "" Then
    MsgBox "Please enter the Number of Member.", vbExclamation, "Input Data"
    Exit Sub
Else
    Dim v As Long, ctrl As Control
    v = txtNoMember.Value
    For i = 1 To v
        For Each ctrl In Controls ' loop through Controls and search for Control with the right name
            If ctrl.Name = "txtMember" & Format(i, "0#") Then
                If ctrl.Value = "" Then
                    MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
                    Exit Sub
                End If
                Exit For
            End If
        Next
    Next
End Sub

This works even if you enter 4 in txtNoMember, when you only have txtMemberNo03 as max.

即使您在 txtNoMember 中输入 4,当您只有 txtMemberNo03 作为最大值时,这也有效。