View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Listbox - Store values

I think I would use a hidden worksheet.

You could use each row to show what was chosen out of your listbox.

But this seemed to work ok for me...

I built a userform with a listbox and 3 buttons (cancel, process this selection,
and Finished).

This is all behind that userform:

Option Explicit
Dim myArray() As Variant
Dim ArrayCtr As Long
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()

Dim aCtr As Long
Dim lbCtr As Long
Dim mySubArray() As Variant

ReDim Preserve mySubArray(1 To Me.ListBox1.ListCount)

With Me.ListBox1
For lbCtr = 1 To .ListCount
mySubArray(lbCtr) = Me.ListBox1.Selected(lbCtr - 1)
.Selected(lbCtr - 1) = False
Next lbCtr
End With

myArray(ArrayCtr) = mySubArray
ReDim Preserve myArray(LBound(myArray) To UBound(myArray) + 1)
ArrayCtr = ArrayCtr + 1
'arrayctr is 1 too many, but so is the ubound(myarray)
'but we're ready for the next time.

End Sub
Private Sub CommandButton3_Click()

Dim aCtr As Long
Dim sCtr As Long

If ArrayCtr = 1 Then
'nothing has ever been chosen
MsgBox "Nothing selected"
Exit Sub 'or unload me
End If

'we have one too many, so don't do the last one!
For aCtr = LBound(myArray) To UBound(myArray) - 1
For sCtr = LBound(myArray(aCtr)) To UBound(myArray(aCtr))
MsgBox "time #: " & aCtr & "--Option#: " _
& sCtr & " was chosen: " & myArray(aCtr)(sCtr)
Next sCtr
Next aCtr

Unload Me

End Sub
Private Sub UserForm_Initialize()
ReDim myArray(1 To 1)
ArrayCtr = 1
Me.CommandButton1.Caption = "Cancel"
Me.CommandButton2.Caption = "Process" & vbLf & "This Choice"
Me.CommandButton3.Caption = "Finished"

With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
.AddItem "A"
.AddItem "B"
.AddItem "C"
.AddItem "D"
End With
End Sub

It essentially makes an array of arrays--each time through a "subarray" is
created of true/falses depending on what they selected.

After that "subarray" is built, it's added as a new member in that bigger array.


=========
If you used a worksheet (hidden), you could clear all the cells when the
userform loads.
Each time the user clicks process, just increment the row number.
Then use each cell in the row to hold True/false depending on what was chosen by
the user.





tjh wrote:

Hello,

I would like to have the values which the user selects from a listbox stored
to memory while the macro is running. Essentially, I want the user to make a
selection from the list. Once they make their selection, use a radial button
to decide if they need to make another selection from the list. If they
choose yes, then the listbox will reappear, allowing them to make their next
selections. Each time I would like their selections to be stored to memory as
a different variable, so that the code can later reference each of their
selections. This seems like it is somewhat redundant and unnecessary, since
they can choose multiple selections at one time. However, each time the
listbox appears the user will be making multiple selections and grouping
them. That is why the box would need to reappear again (for the next group).
The only other way I can think of doing this would be to place a textbox in
the listbox allowing them to group their selections. I do not know how to do
this either, but it would be more efficient.

Any Suggestions

Thank you


--

Dave Peterson