I would like to use a procedure again and again for several listboxes in
a user form.

Therefore, I need to use a variable that can represent the various list
boxes.

Which variable type should I use?

=========================
Private Sub userform_initiate()
Dim oLBox As Object 'Object type DOES NOT WORK
End Sub
-------------------------
Private Sub butBrowse01_click() 'Browse button 01 next to list box 01
oLBox = ThisUserForm.ListBox_01
SelectFiles (False) 'AllowMultiSelect = false
End Sub
-------------------------
Private Sub SelectFiles(bMultSel As Boolean)

Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
With fd
.AllowMultiSelect = bMultSel
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
oLBox.AddItem (vrtSelectedItem) '???
Next
Else
End If
End With
Set fd = Nothing

End Sub
=========================

ANDy

Re: Which variable to represent a listbox? by Helmut

Helmut
Sun Jan 09 07:30:11 CST 2005

Hi Andy,

a maybe somewhat unusual way would be,
to create an array of listboxes.
Unlike VB, at design time, VBA does not propose
to create an array of controls,
when copying and pasting a control.
So, when designing the form and what is on it,
I create, lets say 6 listboxes,
ListBox1 to ListBox6, and assign, one by one,
each listbox to an entry in an array of listboxes.

Like this, but take care of the sequence, in which the listboxes
where created, or check for digits in addition in the controls' name.


' declarations
Dim arList(6) As ListBox
Dim oCtrl As Control
' ---
Private Sub UserForm_Activate()
For Each oCtrl In Me.Controls
If Left(oCtrl.Name, 7) = "ListBox" Then
i = i + 1
Set arList(i) = oCtrl
End If
Next
For i = 1 To 6
arList(i).AddItem "Entry1 " & "list " & CStr(i)
arList(i).AddItem "Entry2 " & "list " & CStr(i)
arList(i).AddItem "Entry3 " & "list " & CStr(i)
Next
End Sub
' ---
Private Sub CommandButton1_Click()
MsgBox arList(4).List(1)
End Sub

HTH

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/

Re: Which variable to represent a listbox? by and

and
Sun Jan 09 08:03:41 CST 2005

Hi Malcolm,

Thanks for your advice. It does work. I used the Listbox variable type,
as you suggested.

The funny thing is that when I hover my mouse pointer over the oLBox
variable after a value is assigned to it, no valus is visible in the
little yellow box.

Best regards,
ANDy
--

Re: Which variable to represent a listbox? by and

and
Sun Jan 09 08:06:41 CST 2005

Hi Helmut,

I understand from Malcolm's reply that I made a scope + Set error. That
is solved now. I will certainly try your method also, since it's simply
great fun to experiment and play with VBA.

Have a nice day,
ANDy
--