View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Greg Wilson Greg Wilson is offline
external usenet poster
 
Posts: 747
Default Msgbox to make a choice

Another way:

This is more complex but IMHO more elegant. It is intended to be used for
the situation where there is a variable number of companies to select from.
The companies must be listed on a worksheet ("Settings"). Typically, this
helper worksheet would be hidden. It creates on-the-fly a userform that that
has the same number of option buttons as companies in the list. The option
button captions are the same as the company names. It was written only
minutes ago and briefly tested so it likely could be improved. It can, of
course, be adapted to suit.

You need to:
1) Paste the code to a standard code module
2) Name a worksheet "Settings"
3) List in sheet "Settings" multiple company names in column A starting at
cell A1
4) Call the macro

The macro will return in a message box the user's selection. It is assumed
that you will do whatever with the result. The userform will self-delete.

Regards,
Greg

Const UFColor As Single = 10040115
Public UserSelection As String
Sub MakeUF()
Dim UF As Object
Dim Ctrl As Object
Dim rng As Range
Dim i As Integer
Dim Code As String

With Sheets("Settings")
Set rng = Intersect(.Range("A1").CurrentRegion, .Columns(1))
End With
Set UF = Application.VBE.ActiveVBProject.VBComponents.Add(3 )
UF.Properties("Width") = 170
UF.Properties("Caption") = "File selection"
With UF.Designer
Set Ctrl = .Controls.Add("Forms.Label.1")
With Ctrl
..Caption = "Select company..."
..ForeColor = UFColor
..Top = 5
..Left = 5
..Height = 15
..Width = 200
End With
For i = 1 To rng.Count
Set Ctrl = .Controls.Add("Forms.OptionButton.1")
With Ctrl
..Caption = rng(i, 1).Value
..ForeColor = UFColor
..Top = 20 + (i - 1) * 15
..Left = 5
..Height = 15
..Width = 150
..Value = (i = 1)
End With
Next
Set Ctrl = .Controls.Add("Forms.CommandButton.1")
With Ctrl
..Caption = "Apply"
..ForeColor = UFColor
..Top = 30 + (i - 1) * 15
..Left = 5
..Height = 20
..Width = 75
End With
Set Ctrl = .Controls.Add("Forms.CommandButton.1")
With Ctrl
..Caption = "Cancel"
..ForeColor = UFColor
..Top = 30 + (i - 1) * 15
..Left = 85
..Height = 20
..Width = 75
End With
End With

UF.Properties("Height") = Ctrl.Top + 45

Code = "Private Sub CommandButton1_Click()"
Code = Code & vbCrLf & "Dim i As Integer"
Code = Code & vbCrLf & "For i = 1 To Me.Controls.Count - 3"
Code = Code & vbCrLf & "If Me.Controls(i) = True Then"
Code = Code & vbCrLf & "UserSelection = Me.Controls(i).Caption"
Code = Code & vbCrLf & "Exit For"
Code = Code & vbCrLf & "End If"
Code = Code & vbCrLf & "Next"
Code = Code & vbCrLf & "Unload Me"
Code = Code & vbCrLf & "End Sub"
Code = Code & vbCrLf & "Private Sub CommandButton2_Click()"
Code = Code & vbCrLf & "Unload Me"
Code = Code & vbCrLf & "End Sub"
UF.CodeModule.InsertLines 2, Code

VBA.UserForms.Add(UF.Name).Show
ThisWorkbook.VBProject.VBComponents.Remove UF
MsgBox UserSelection
End Sub