我正在尝试向userform动态添加按钮,但userform只是空白。我尽可能地简化了代码的本质,以便进行错误检查(不是它对我有帮助)
Sub addLabel()
UserForm2.Show
Dim theLabel As Label
Dim labelCounter As Integer
For labelCounter = 1 To 3
Set Label = UserForm2.Controls.Add("Forms.Label.1", "Test" & labelCounter, True)
With theLabel
.Caption = "Test" & labelCounter
.Left = 10
.Width = 50
.Top = 10
End With
End Sub
有没有办法检查按钮是否已添加但是不可见?或者为什么他们没有被添加。非常感谢任何帮助。
答案 0 :(得分:22)
一些事情:
vbModeless
- 否则代码会停在UserForm2.Show
Label
的对象,然后在With
上使用theLabel
然后,您需要增加三个标签的位置以避免重叠(我使用Top
完成)。
Sub addLabel()
UserForm2.Show vbModeless
Dim theLabel As Object
Dim labelCounter As Long
For labelCounter = 1 To 3
Set theLabel = UserForm2.Controls.Add("Forms.Label.1", "Test" & labelCounter, True)
With theLabel
.Caption = "Test" & labelCounter
.Left = 10
.Width = 50
.Top = 10 * labelCounter
End With
Next
End Sub
答案 1 :(得分:0)
在end with
语句之后,添加:
userform1.show
又一次更正:
.top = 10*labelcounter+10
答案 2 :(得分:-4)
尝试以下代码
Set theLabel = UserForm2.Designer.Controls.Add("Forms.Label.1", "Test1", True)