vba 循环浏览一组文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5111073/
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-11 12:44:06  来源:igfitidea点击:

Looping through a collection of text boxes

vbaexcel-vbaexcel

提问by cmerrell

I have a userform that has three conceptual groups of textboxes. I'm trying to create a collection for each group and then when the user clicks on a button to call a sub/function associated with one of those groups, I want to be able to call a function that loops through the collection of textboxes associated with that group and check if they are empty, contain invalid characters, etc.

我有一个用户表单,其中包含三个概念性的文本框组。我正在尝试为每个组创建一个集合,然后当用户单击一个按钮来调用与这些组之一关联的子/函数时,我希望能够调用一个循环关联文本框集合的函数与该组并检查它们是否为空,是否包含无效字符等。

I've made the following declarations at the module level.

我在模块级别做了以下声明。

Dim typSectFields, laneFields, matFields As Collection

Then when the user form initializes I add the text boxes to the collections:

然后当用户表单初始化时,我将文本框添加到集合中:

Set typSectFields = New Collection

    With frmAddTypSect

        typSectFields.Add txtTypSectName
        typSectFields.Add txtStartSta
        typSectFields.Add txtEndSta
    End With

And then when the user clicks the button that uses the input from the "typSectFields" collection:

然后当用户单击使用“typSectFields”集合中的输入的按钮时:

Dim tb As Control, res As VbMsgBoxResult

For Each tb In typSectFields
        If tb.Text = vbNullString And t.Tag <> vbNullString Then

            res = MsgBox("You've not completed the " + tb.Tag + " field. Would you like to complete it now?", vbYesNo + vbQuestion)

            If res = vbYes Then Exit Sub

        End If
Next

I get an "Object Required" error when execution hits the For loop.
VBE shows that tb = nothing and typSectFields = Empty.

当执行遇到 For 循环时,我收到“需要对象”错误。
VBE 显示 tb = nothing 和 typSectFields = Empty。

What am I doing wrong?

我究竟做错了什么?

采纳答案by mwolfe02

Make sure all of your code (specifically the module-level declarations) are in the code behind the form module. The following runs without error for me:

确保所有代码(特别是模块级声明)都在表单模块后面的代码中。以下运行对我来说没有错误:

Dim typSectFields As Collection, laneFields As Collection, matFields As Collection

Private Sub CommandButton1_Click()
Dim tb As Control, res As VbMsgBoxResult

    For Each tb In typSectFields
        If tb.Text = vbNullString And tb.Tag <> vbNullString Then
            res = MsgBox("You've not completed the " + tb.Tag + " field. Would you like to complete it now?", vbYesNo + vbQuestion)

            If res = vbYes Then Exit Sub

        End If
    Next
End Sub

Private Sub UserForm_Initialize()
    Set typSectFields = New Collection

    With frmAddTypSect
        typSectFields.Add txtTypSectName
        typSectFields.Add txtStartSta
        typSectFields.Add txtEndSta
    End With

End Sub