Sample code to produce a userform with OptionButtons to run code
'---------------------------------------------------------------------------------------
' DateTime : 09/05/2007 08:43
' Author : Roy Cox (royUK)
' Website : click here for more examples and Excel Consulting
' Purpose : Add a temporaryform to with Option Buttons to call macros'
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
Option Explicit
Dim uFrm
Dim x As Integer
Dim iX As Integer
'---------------------------------------------------------------------------------------
' Procedure : CreateForm
' DateTime : 25/05/2009 13:43
' Author : Roy Cox
' Website : click here for more examples and Excel Consulting
' Purpose : Main code to create the form, run from button or menu
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
'
Sub CreateForm()
'run this macro to create the form, this will call the other procedures in the right order
On Error GoTo exit_handler
With Application
.VBE.MainWindow.Visible = False
'designing UserForm1
uFrm_Create
uFrm_Controls_Create
uFrm_Codes
uFrm_Show
uFrm_Remove 'To Keep userform1 then comment this line
End With
End Sub
'---------------------------------------------------------------------------------------
' Procedure : uFrm_Create
' DateTime : 25/05/2009 13:42
' Author : Roy Cox
' Website : click here for more examples and Excel Consulting
' Purpose : Add temporary Form
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
'
Function uFrm_Create()
uFrm_Remove ' If Userform1 exist then remove it.
With ThisWorkbook.VBProject.VBComponents
Set uFrm = .Add(3).Designer ' Create & set form designer
End With
End Function
'---------------------------------------------------------------------------------------
' Procedure : uFrm_Controls_Create
' DateTime : 25/05/2009 13:41
' Author : Roy Cox
' Website : click here for more examples and Excel Consulting
' Purpose : Add Controls to the Form
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
'
Function uFrm_Controls_Create()
Dim Opt As MSForms.Control
With uFrm.Controls
x = 5
For iX = 1 To 5
Set Opt = .Add("Forms.OptionButton.1")
With Opt
.Top = x
.Left = 25
.Width = 90
.Visible = True
.Caption = Choose(iX, "macro1", "macro2", "macro3", "macro4", "macro5")
End With
x = x + 35
Next iX
End With
Set Opt = Nothing
End Function
'---------------------------------------------------------------------------------------
' Procedure : uFrm_Codes
' DateTime : 25/05/2009 13:40
' Author : Roy Cox
' Website : click here for more examples and Excel Consulting
' Purpose : Add code to CommandButton
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
'
Function uFrm_Codes()
With ThisWorkbook.VBProject.VBComponents("Userform1").CodeModule
.InsertLines 2, "Sub OptionButton1_Click()"
.InsertLines 3, "macro1"
.InsertLines 4, "End Sub"
.InsertLines 5, "Sub OptionButton2_Click()"
.InsertLines 6, "macro2"
.InsertLines 7, "End Sub"
.InsertLines 8, "Sub OptionButton3_Click()"
.InsertLines 9, "macro3"
.InsertLines 10, "End Sub"
.InsertLines 11, "Sub OptionButton4_Click()"
.InsertLines 12, "macro4"
.InsertLines 13, "End Sub"
.InsertLines 14, "Sub OptionButton5_Click()"
.InsertLines 15, "macro5"
.InsertLines 16, "End Sub"
End With
End Function
'---------------------------------------------------------------------------------------
' Procedure : uFrm_Show
' DateTime : 25/05/2009 13:40
' Author : Roy Cox
' Website : click here for more examples and Excel Consulting
' Purpose : Set Form Properties & display form
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
'
Function uFrm_Show()
With UserForm1
.Caption = "Run a macro"
.Height = x * 1.05
.Width = 50
.Show
End With
End Function
'---------------------------------------------------------------------------------------
' Procedure : uFrm_Remove
' DateTime : 25/05/2009 13:41
' Author : Roy Cox
' Website : click here for more examples and Excel Consulting
' Purpose : Delete the Form
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
'
Function uFrm_Remove()
On Error Resume Next
With ThisWorkbook.VBProject
.VBComponents.Remove .VBComponents("Userform1")
End With
Err.Clear
End Function
Bookmarks