ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Storing the results of a list box (https://www.excelbanter.com/excel-programming/333518-storing-results-list-box.html)

Simon Shaw

Storing the results of a list box
 
What is the best method of storing the selected results of a listbox? to a
variable?

I am calling a userform from another userform and I need to pass back the
selections of a listbox to the first userform, not for display but rather for
later processing.

I have tried declaring a public variable as type ListBox, then tried

Set PublicListBox = ListBox

i get a type mismatch error, an ideas?

Thanks

Simon Shaw

Bob Phillips[_7_]

Storing the results of a list box
 
On the first userform, declare a public string

Public myVal As String

In the second, set it say on the Listbox click

Userform1.myVal = Me.Listbox1.Value

--
HTH

Bob Phillips

"Simon Shaw" <simonATsimonstoolsDOTcom wrote in message
...
What is the best method of storing the selected results of a listbox? to a
variable?

I am calling a userform from another userform and I need to pass back the
selections of a listbox to the first userform, not for display but rather

for
later processing.

I have tried declaring a public variable as type ListBox, then tried

Set PublicListBox = ListBox

i get a type mismatch error, an ideas?

Thanks

Simon Shaw




Simon Shaw

Storing the results of a list box
 
This doesn't work as he Listbox is a multiselect.

I also want to be able to re-populate the listbox with the previous
selection if the user opens the second userform again.

Thanks

"Bob Phillips" wrote:

On the first userform, declare a public string

Public myVal As String

In the second, set it say on the Listbox click

Userform1.myVal = Me.Listbox1.Value

--
HTH

Bob Phillips

"Simon Shaw" <simonATsimonstoolsDOTcom wrote in message
...
What is the best method of storing the selected results of a listbox? to a
variable?

I am calling a userform from another userform and I need to pass back the
selections of a listbox to the first userform, not for display but rather

for
later processing.

I have tried declaring a public variable as type ListBox, then tried

Set PublicListBox = ListBox

i get a type mismatch error, an ideas?

Thanks

Simon Shaw





William Benson[_2_]

Storing the results of a list box
 
Hi Simon,

Try saving to the dynamic Array I have named Ar():


Private Sub ListBox1_Change()
Dim i As Long
Dim Ar() As String

ReDim Ar(0)

With Me.ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
ReDim Preserve Ar(UBound(Ar) + 1)
Ar(UBound(Ar)) = .List(i)
End If
Next i
End With
End Sub


"Simon Shaw" <simonATsimonstoolsDOTcom wrote in message
...
This doesn't work as he Listbox is a multiselect.

I also want to be able to re-populate the listbox with the previous
selection if the user opens the second userform again.

Thanks

"Bob Phillips" wrote:

On the first userform, declare a public string

Public myVal As String

In the second, set it say on the Listbox click

Userform1.myVal = Me.Listbox1.Value

--
HTH

Bob Phillips

"Simon Shaw" <simonATsimonstoolsDOTcom wrote in message
...
What is the best method of storing the selected results of a listbox?
to a
variable?

I am calling a userform from another userform and I need to pass back
the
selections of a listbox to the first userform, not for display but
rather

for
later processing.

I have tried declaring a public variable as type ListBox, then tried

Set PublicListBox = ListBox

i get a type mismatch error, an ideas?

Thanks

Simon Shaw







LenB[_3_]

Storing the results of a list box
 
While trying this I came up with very similar code to what William
Benson had. In order to have the first form see the array, I had to
declare it public in a code module, like Public Ar() As String . Not
sure if there is any other way. Couldn't make it public in a form.
Sometimes if I need data to be shared, I store it to cells in a
worksheet, then every module, form and control can see it.

To re-populate the listbox with the results, try this

'Somewhere in the first form, before showing the second form.
'Note that the array must have been redim'ed and have values.
Dim i As Integer
if <condition to repopulate list box then
Form2.ListBox1.Clear
For i = 1 To UBound(Ar)
Form2.ListBox1.AddItem (Ar(i))
Next
endif
Form2.Show


Len


Simon Shaw wrote:
This doesn't work as he Listbox is a multiselect.

I also want to be able to re-populate the listbox with the previous
selection if the user opens the second userform again.

Thanks


Bob Phillips[_7_]

Storing the results of a list box
 
Actually, I was being dumb, you don't need a v ariable, yopu could access
the control directly.

Similalrly, if it multi-select, you could in Userform2 use

Dim i As Long

With UserForm1.ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
MsgBox .Value
End If
Next i
End With


--
HTH

Bob Phillips

"Simon Shaw" <simonATsimonstoolsDOTcom wrote in message
...
This doesn't work as he Listbox is a multiselect.

I also want to be able to re-populate the listbox with the previous
selection if the user opens the second userform again.

Thanks

"Bob Phillips" wrote:

On the first userform, declare a public string

Public myVal As String

In the second, set it say on the Listbox click

Userform1.myVal = Me.Listbox1.Value

--
HTH

Bob Phillips

"Simon Shaw" <simonATsimonstoolsDOTcom wrote in message
...
What is the best method of storing the selected results of a listbox?

to a
variable?

I am calling a userform from another userform and I need to pass back

the
selections of a listbox to the first userform, not for display but

rather
for
later processing.

I have tried declaring a public variable as type ListBox, then tried

Set PublicListBox = ListBox

i get a type mismatch error, an ideas?

Thanks

Simon Shaw








All times are GMT +1. The time now is 03:50 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com