Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 553
Default Passing multiselections from listbox to sub

I have a listbox where the selected item is passed to a sub:

NewSub(listbox1.Value)

I want to turn on the multiselect and pass all selected values to the same
sub. Is the only way of doing this by sending the multiselect items to an
array and pass the array to the sub?

If so, how do you pass the items from the listbox to the array.

Thanks

EM

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Passing multiselections from listbox to sub

That seems like a reasonable approach to me.

Another option would be to just pass the listbox itself to the other function.
And look for the selected items there.

The first option...

I created a userform with a listbox and two command buttons on it. All this
code was behind the userform:

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
Dim iCtr As Long
Dim sCtr As Long
Dim myArr() As String
With Me.ListBox1
ReDim myArr(0 To .ListCount - 1)
sCtr = -1
For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) Then
sCtr = sCtr + 1
myArr(sCtr) = .List(iCtr)
End If
Next iCtr
End With

If sCtr = -1 Then
'nothing selected, don't call anything
Beep
Else
ReDim Preserve myArr(0 To sCtr)
Call NewFunction(myArr)
End If
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
Me.ListBox1.MultiSelect = fmMultiSelectMulti
For iCtr = 1 To 10
Me.ListBox1.AddItem "asdf" & iCtr
Next iCtr
End Sub
Function NewFunction(myArr() As String)
Dim iCtr As Long
For iCtr = LBound(myArr) To UBound(myArr)
MsgBox myArr(iCtr)
Next iCtr
End Function

=======
The alternative--passing the listbox itself (with the same userform):

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
Call NewFunction(Me.ListBox1)
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
Me.ListBox1.MultiSelect = fmMultiSelectMulti
For iCtr = 1 To 10
Me.ListBox1.AddItem "asdf" & iCtr
Next iCtr
End Sub
Function NewFunction(myListbox As MSForms.ListBox)
Dim iCtr As Long
With myListbox
For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) Then
MsgBox .List(iCtr)
End If
Next iCtr
End With
End Function


ExcelMonkey wrote:

I have a listbox where the selected item is passed to a sub:

NewSub(listbox1.Value)

I want to turn on the multiselect and pass all selected values to the same
sub. Is the only way of doing this by sending the multiselect items to an
array and pass the array to the sub?

If so, how do you pass the items from the listbox to the array.

Thanks

EM


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Userform Listbox to sub causes type-mismatch error Kleev Excel Programming 2 October 19th 05 08:46 PM
Passing the value of listbox to other Subs Cygnus241 Excel Programming 1 September 14th 05 05:14 AM
Passing User Defined Type Array to Listbox PC[_4_] Excel Programming 2 June 1st 05 02:44 AM
listbox.value not equal to listbox.list(listbox.listindex,0) ARB Excel Programming 0 October 22nd 03 12:46 AM


All times are GMT +1. The time now is 10:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"