The following code adds Labels to Userform1. However all the Labels appear on top of each other at the top left of the userform. What might the problem be?
Code:
Sub AddControls()
Dim UFvbc As VBComponent
Dim c, lb As Control
Dim r As Long
Dim x As VBIDE.VBProject
' Make sure access to the VBProject is allowed
On Error Resume Next
Set x = Workbooks(1).VBProject
If Err <> 0 Then
MsgBox "Your security settings do not allow this macro to run.", vbCritical
Exit Sub
End If
Set UFvbc = x.VBComponents("Userform1")
' Delete all controls, if any
For Each c In UFvbc.Designer.Controls
UFvbc.Designer.Controls.Remove c.Name
Next c
' Add Labels
For r = 1 To 10
Set lb = UFvbc.Designer.Controls.Add("Forms.label.1")
With lb
.Width = 25
.Height = 25
.Left = 10
.Top = (r * .Height) + 35
.Caption = Worksheets(3).Cells(r, 1)
.Tag = "ItemNo " & r
.Name = "lbItemNo" & r
End With
'Initialise the userform
VBA.UserForms.Add ("Userform1")
End Sub
Thanks for your help
Bookmarks