Sometime we need to Create UserForm Component at Run Time , We can do this By Following Codes :
Sub AddInRunTime()
'On Error Resume Next
Dim Cb As Control
ThisWorkbook.VBProject.VBComponents.Add vbext_ct_MSForm
Set Cb = ThisWorkbook.VBProject.VBComponents("UserForm1").Designer.Controls.Add("Forms.CommandButton.1")
Cb.Left = 50
Cb.Top = 50
Cb.Caption = "Click Me"
st = "Private Sub CommandButton1_Click()" & vbCrLf & _
"MsgBox " & """I Born in Run Time""" & ", vbInformation " & vbCrLf & _
"End Sub"
ThisWorkbook.VBProject.VBComponents("UserForm1").CodeModule.InsertLines 1, st
Call Show
End Sub
Sub Show()
UserForm1.Show
End Sub
Sub AddInRunTime()
'On Error Resume Next
Dim Cb As Control
ThisWorkbook.VBProject.VBComponents.Add vbext_ct_MSForm
Set Cb = ThisWorkbook.VBProject.VBComponents("UserForm1").Designer.Controls.Add("Forms.CommandButton.1")
Cb.Left = 50
Cb.Top = 50
Cb.Caption = "Click Me"
st = "Private Sub CommandButton1_Click()" & vbCrLf & _
"MsgBox " & """I Born in Run Time""" & ", vbInformation " & vbCrLf & _
"End Sub"
ThisWorkbook.VBProject.VBComponents("UserForm1").CodeModule.InsertLines 1, st
Call Show
End Sub
Sub Show()
UserForm1.Show
End Sub
No comments:
Post a Comment