listbox - questions

B

bryan

I curently have been using combo boxes in userforms for users to select one
item from a list. What I also want to do is supply a list of endorsement
forms and allow them the ability to select multiple forms to be included with
the form.

Can I use this same code and use listbox
or do I need to have a combo box loaded with the selections and an OK button
to load them to a listbox?
And then another OK button when complete?
If this then how do I get the values of the listbox?

Here is code I use for combobox:
'to call userform
Set UF = New UserForm1
UF.Show
strrec = UF.ComboBox1.Text
Set UF = Nothing
End If

'userform
Sub UserForm_Initialize()
ComboBox1.AddItem ("80%")
ComboBox1.AddItem ("100%")
ComboBox1.ListIndex = 0
End Sub
Sub CommandButton1_Click()
Me.Hide
End Sub

I appreciate the help,
Bryan
 
J

Jean-Guy Marcil

bryan said:
I curently have been using combo boxes in userforms for users to select one
item from a list. What I also want to do is supply a list of endorsement
forms and allow them the ability to select multiple forms to be included with
the form.

Can I use this same code and use listbox
or do I need to have a combo box loaded with the selections and an OK button
to load them to a listbox?
And then another OK button when complete?
If this then how do I get the values of the listbox?

Here is some code that takes the selected values (if any) from a listbox and
types them "as is" at the current selection in the currently active document.
Note that when building the userfrom, you must set the listbox "multiSelect"
property to "frmMultiSelectMulti", or do it in the initialize event.

Main code:

Option Explicit

Sub Test()

Dim UF As UserForm1
Dim varRec As Variant
Dim strRec As String
Dim i As Long
Dim boolSelected As Boolean

Set UF = New UserForm1
boolSelected = False

With UF
.Show
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) Then
If Not boolSelected Then
strRec = .ListBox1.List(i)
boolSelected = True
Else
strRec = strRec & "|" & .ListBox1.List(i)
End If
End If
Next
If boolSelected Then
varRec = Split(strRec, "|")
End If
End With

Set UF = Nothing

If Not boolSelected Then
Selection.TypeText "Nothing was selected."
Else
For i = LBound(varRec) To UBound(varRec)
Selection.TypeText varRec(i) & vbCrLf
Next
End If

End Sub


Userform code:

Option Explicit

Sub UserForm_Initialize()

With Me.ListBox1
.AddItem ("40%")
.AddItem ("60%")
.AddItem ("80%")
.AddItem ("100%")
.ListIndex = 0
End With

End Sub

Sub CommandButton1_Click()

Me.Hide

End Sub
 
B

bryan

Works like a charm.
I had modify a bit as I am using the selected items to go out and insert a
file into the document. I don't wan't the selections on the form per se.

Thanks much for the help and the quick response,
Bryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top