1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| Option Explicit
' Références à cocher
' Microsoft Visual Basic for Applications Extensibility 5.3
' Microsoft Forms 2.0 Object Library
Sub UserForm_Dynamique()
Dim Usf As Object
Dim txtB As MSForms.TextBox
Dim Cb As MSForms.CommandButton
Dim sStr As String
Dim i As Long, k As Long
Set Usf = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With Usf
.Properties("Caption") = "UserForm Dynamique"
.Properties("Height") = 240
.Properties("Width") = 320
End With
Set Cb = Usf.Designer.Controls.Add("forms.CommandButton.1")
With Cb
.Caption = "Fermer"
.Left = 200
.Top = 180
End With
sStr = "Sub CommandButton1_Click()" & _
vbCrLf & " Unload Me" & _
vbCrLf & "End Sub" & vbCrLf
With Usf.CodeModule
i = .CountOfLines
.InsertLines i + 1, sStr
End With
For k = 1 To 5
Set txtB = Usf.Designer.Controls.Add("forms.Textbox.1")
With txtB
.Tag = k
.Text = "texte" & k
.TextAlign = fmTextAlignCenter
.Left = 20
.Top = 20 * k
.BackColor = vbGreen
.BorderStyle = fmBorderStyleSingle
End With
Next k
For k = 1 To 5
sStr = "Sub TextBox" & k & "_DblClick(ByVal Cancel As MSForms.ReturnBoolean)" & vbCrLf & _
" Msgbox(" & "TextBox" & k & ".Tag)" & _
vbCrLf & "End Sub" & vbCrLf
With Usf.CodeModule
i = .CountOfLines
.InsertLines i + 1, sStr
End With
Next k
VBA.UserForms.Add(Usf.Name).Show
ThisWorkbook.VBProject.VBComponents.Remove Usf
End Sub |
Partager