找到动态控件的底部位置

时间:2014-08-28 13:47:26

标签: vba excel-vba excel

在我的userform中,我询问用户他们需要多少列表框,并根据用户输入控件的数量在运行时动态创建。我希望能够根据添加到用户窗体的最后一个动态控件的底部位置设置我的用户窗体的大小。下面显示了为完成此操作而编写的代码,我想要在每分钟完成的操作是警告每个动态控件添加到用户表单时的底部位置。

Dim dynamicControl As Control

For i = 1 To TextBox1.Value
    Set cList = Me.Controls.Add("Forms.ListBox.1")
    With cList
       .Name = "listbox" & (i)
       .Left = 150
       .Top = listStartPosition
       .Width = 300
       .Height = 140
    End With
Next i

dynamicControl = "listbox" & (i) 
Msgbox dynamicControl.Bottom

当我运行我的代码时,当我尝试设置dynamicControl = "listbox" & 0并且我收到的错误是object variable or with block variable not set对象变量或未设置块变量

时,它会出错

1 个答案:

答案 0 :(得分:-2)


要获得动态高度,你可以使用这样的代码,通过发送clist的数量,这段代码将创建它们:

Private Sub createcontrol(num As Integer)
    Dim StartPos As Integer
    Dim WidthPos As Integer
    StartPos = TextBox1.Top + TextBox1.Height + 10
    For i = 1 To num
        Set cList = Me.Controls.Add("Forms.ListBox.1")
        With cList
            .Name = "listbox" & (i)
            .Left = 150
            .Top = StartPos
            .Width = 300
            .Height = 140
            StartPos = StartPos + .Height + 10
            Me.Width = .Width + .Left
        End With
    Next i
    Me.Height = StartPos - 8

End Sub