excel vba中能否动态创建控件
我想创建一组复选框,是跟据EXCEL表格里的数据来创建的,就是要计算才知道要多少个想用VBA弹出一个窗口,然后在窗口上动态创建,请问能否实现VB就可以轻易实现比如Dimi...
我想创建一组复选框,是跟据EXCEL表格里的数据来创建的,就是要计算才知道要多少个
想用VBA弹出一个窗口,然后在窗口上动态创建,请问能否实现
VB就可以轻易实现
比如
Dim i As Integer
For i = 1 To 3
Load Check1(i)
Check1(i).Caption = "Check" & CStr(i + 1)
Check1(i).Top = Check1(i - 1).Top + 500
Check1(i).Left = Check1(0).Left
Check1(i).Visible = True
Next
但是在VBA在就提示出错
有没高手帮个忙
究竟是不支持这样的功能还是另有方法 展开
想用VBA弹出一个窗口,然后在窗口上动态创建,请问能否实现
VB就可以轻易实现
比如
Dim i As Integer
For i = 1 To 3
Load Check1(i)
Check1(i).Caption = "Check" & CStr(i + 1)
Check1(i).Top = Check1(i - 1).Top + 500
Check1(i).Left = Check1(0).Left
Check1(i).Visible = True
Next
但是在VBA在就提示出错
有没高手帮个忙
究竟是不支持这样的功能还是另有方法 展开
2个回答
展开全部
把下面的代码添加到Userform下面的Initialize事件中,也就是窗体启动时候做的事。
Private Sub UserForm_Initialize()
Dim ChkBox As Control
Dim I As Integer
For I = 1 To 3
Set ChkBox = UserForm1.Controls.Add("Forms.CheckBox.1", "Chk" & Str(I))
ChkBox.Top = 2 + 25 * (I - 1)
ChkBox.Height = 20
ChkBox.Width = 100
ChkBox.Left = 2
ChkBox.Caption = "I am ChkBox" & Str(I)
Set ChkBox = Nothing
Next
End Sub
或者,你也可以把这段代码添加到一个过程中如下,但是记住,Showform一定要在你添加控件之后。
Sub test()
Dim ChkBox As Control
Dim I As Integer
For I = 1 To 3
Set ChkBox = UserForm1.Controls.Add("Forms.CheckBox.1", "Chk" & Str(I))
ChkBox.Top = 2 + 25 * (I - 1)
ChkBox.Height = 20
ChkBox.Width = 100
ChkBox.Left = 2
ChkBox.Caption = "I am ChkBox" & Str(I)
Set ChkBox = Nothing
Next
UserForm1.Show
End Sub
Private Sub UserForm_Initialize()
Dim ChkBox As Control
Dim I As Integer
For I = 1 To 3
Set ChkBox = UserForm1.Controls.Add("Forms.CheckBox.1", "Chk" & Str(I))
ChkBox.Top = 2 + 25 * (I - 1)
ChkBox.Height = 20
ChkBox.Width = 100
ChkBox.Left = 2
ChkBox.Caption = "I am ChkBox" & Str(I)
Set ChkBox = Nothing
Next
End Sub
或者,你也可以把这段代码添加到一个过程中如下,但是记住,Showform一定要在你添加控件之后。
Sub test()
Dim ChkBox As Control
Dim I As Integer
For I = 1 To 3
Set ChkBox = UserForm1.Controls.Add("Forms.CheckBox.1", "Chk" & Str(I))
ChkBox.Top = 2 + 25 * (I - 1)
ChkBox.Height = 20
ChkBox.Width = 100
ChkBox.Left = 2
ChkBox.Caption = "I am ChkBox" & Str(I)
Set ChkBox = Nothing
Next
UserForm1.Show
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |