VBA 控件集合(数组?)

Sha*_*son 4 excel controls vba textbox class

在寻找一种在用户表单上模拟可填充网格的方法时,我在 Excel 先生网站上发现了这一点:

Dim Grid(1 To 10, 1 To 5) As MSForms.TextBox

Private Sub UserForm_Initialize()

Dim x As Long
Dim y As Long

For x = 1 To 10
    For y = 1 To 5
        Set Grid(x, y) = Me.Controls.Add("Forms.Textbox.1")
        With Grid(x, y)
            .Width = 50
            .Height = 20
            .Left = y * .Width
            .Top = x * .Height
            .SpecialEffect = fmSpecialEffectFlat
            .BorderStyle = fmBorderStyleSingle
        End With
    Next y
Next x

End Sub
Run Code Online (Sandbox Code Playgroud)

我觉得这太棒了。不要告诉我的客户,但我不知道您可以使用Dim Groupname(1 to x, 1 to y)As MSForms.TextBox 创建这样的文本框“数组”。

我试图了解更多相关信息,但搜索“Array of Controls”并没有让我找到此功能。所以我在这里问:

  1. “阵列”是这个能力的真正术语吗?(这样我就可以更好地搜索更多信息。)
  2. 这个“网格”中的所有控件都是文本框。我假设我可以对一组标签、按钮等执行相同的操作。但是有没有办法包含不同类型的控件?(例如,我希望第一列是标签,最后一列是组合框)

Pᴇʜ*_*Pᴇʜ 5

您可以像下面这样做:

Option Explicit

Dim Grid(1 To 10, 1 To 5) As Object  ' declare as object so it can take any control you like

Private Sub UserForm_Initialize()
    Dim iCol As Long
    For iCol = LBound(Grid, 2) To UBound(Grid, 2)  ' loop through all columns 1 to 5

        Dim iRow As Long
        For iRow = LBound(Grid, 1) To UBound(Grid, 1)  ' loop through all rows 1 to 10

            Select Case iCol                
                Case LBound(Grid, 2)  ' first column
                    Set Grid(iRow, iCol) = Me.Controls.Add("Forms.Label.1")
                    With Grid(iRow, iCol)
                        .Width = 50
                        .Height = 20
                        .Left = iCol * .Width
                        .Top = iRow * .Height
                        .Caption = iRow
                        .SpecialEffect = fmSpecialEffectFlat
                        .BorderStyle = fmBorderStyleSingle
                    End With
                    
                Case UBound(Grid, 2)  ' last column
                    Set Grid(iRow, iCol) = Me.Controls.Add("Forms.Combobox.1")
                    With Grid(iRow, iCol)
                        .Width = 50
                        .Height = 20
                        .Left = iCol * .Width
                        .Top = iRow * .Height
                        .SpecialEffect = fmSpecialEffectFlat
                        .BorderStyle = fmBorderStyleSingle
                    End With
                    
                Case Else  ' all other columns
                    Set Grid(iRow, iCol) = Me.Controls.Add("Forms.Textbox.1")
                    With Grid(iRow, iCol)
                        .Width = 50
                        .Height = 20
                        .Left = iCol * .Width
                        .Top = iRow * .Height
                        .SpecialEffect = fmSpecialEffectFlat
                        .BorderStyle = fmBorderStyleSingle
                    End With
                    
            End Select
        Next iRow
    Next iCol
End Sub
Run Code Online (Sandbox Code Playgroud)

但是您需要在循环中按列进行工作。因此,首先是列循环,然后是行循环。使用 ,Select Case您可以将列从标签切换到组合框再到文本框。

因此,在您的情况下,您仍然有 5 列,每列 10 个控件,但第一列是标签,最后一列是组合框:

在此输入图像描述