View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Excel vba UserForm Lists not registering default values?

Hi,

I have 3 lists on my UserForm, all initiated as follows:

List.Clear
With List
.addItem "a"
.addItem "b"
etc.
End With

At the very end, I have:
List1.Value = "a"
List2.Value = "b"
List3.Value = "c"

Msgbox List1.Value & List2.Value & List3.Value

Currently List2.Value returns a blank - I've also had it switch to
List1 being blank between loads (no code touched, just saving,
exporting to test the form, and exiting before going back in)
EDIT: After running it repeatedly without exiting, I find that it
just seems to randomize between which ones show up at all even
without exiting...

However, it registers the default value of the list selection -
whichever value I set those lists to is what the list starts off
having selected. It just doesn't seem to want to show the value in
the Msgbox, and subsequently, anything I try to use that initial
value in.

Any clue what's going on?

Exact Code:

Code:
--------------------


Private Sub UserForm_Initialize()

ListBox1.Clear
ListBox2.Clear
ListBox3.Clear

With ListBox1
.AddItem "a"
.AddItem "b"
.AddItem "c"
End With

With ListBox2
.AddItem "a"
.AddItem "b"
.AddItem "c"
End With

With ListBox3
.AddItem "a"
.AddItem "b"
.AddItem "c"
End With

ListBox1.Value = "b"
ListBox2.Value = "c"
ListBox3.Value = "a"

MsgBox ListBox1.Value & ListBox2.Value & ListBox3.Value

End Sub


--------------------


What are you calling a "userform list"? If this is a ListBox control
then 'Value' is the property that returns the value of its
*BoundColumn* when a list item is selected.

Using your example, I put 3 ListBox controls on a userform and selected
1 item from each (a,b,c), then clicked the userform and got this...

a,b,c

...where the controls were initialized as follows:

Private Sub UserForm_Click()
MsgBox Me.ListBox1.Value & "," _
& Me.ListBox2.Value & "," _
& Me.ListBox3.Value
End Sub

Private Sub UserForm_Initialize()
Me.ListBox1.List = Array("a", "b", "c")
Me.ListBox2.List = Array("a", "b", "c")
Me.ListBox3.List = Array("a", "b", "c")
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion