PDA

View Full Version : Userform Intializing ListBoxes with Default Value



bludragonner
07-06-2016, 11:40 AM
I am attempting to create a userform and populate a listbox with a few different options. The form then should set one of the options as a default value and the user should be able to submit the userform not doing anything (assuming that the default value is desired) I have the following code in the initialize section of the userform.

I check all fields on the userform to just make sure that each field has a value and that the user hasn't unset any values somehow. For some reason, the listbox doesn't return a value, or rather returns a blank value. However, one of the listboxes, ListBox1, seems to work fine.




Private Sub UserForm_Initialize()
With ListBox1
.AddItem "0.5 oz"
.AddItem "1.0 oz"
.Value = "0.5 oz"
End With


With ListBox2
.AddItem "0.5 oz"
.AddItem "1.0 oz"
.Value = "1.0 oz"
End With
End Sub

Private Sub CommandButton1_Click()
If ListBox1.Value = "" Or ListBox2.Value = "" Then
MsgBox "One of the listboxes is not set"
End If
End Sub

SamT
07-06-2016, 12:13 PM
.Listindex = 0

gmaxey
07-06-2016, 12:33 PM
Private Sub UserForm_Initialize()
With ListBox1
.AddItem "0.5 oz"
.AddItem "1.0 oz"
.ListIndex = 0
End With
With ListBox2
.List = ListBox1.List
.ListIndex = 1
End With
End Sub

bludragonner
07-06-2016, 02:18 PM
I tried that. Still producing the same error. ListBox2.Value gives me a good value but ListBox1.Value is not outputting anything.

My code now is:

Private Sub UserForm_Initialize()
With ListBox1
.AddItem "0.5 oz"
.AddItem "1.0 oz"
.ListIndex = 1
End With


With ListBox2
.AddItem "0.5 oz"
.AddItem "1.0 oz"
.ListIndex = 0
End With
End Sub

Private Sub CommandButton1_Click()
MsgBox "ListBox1 Value: " & ListBox1.Value
MsgBox "ListBox2 Value: " & ListBox2.Value
End Sub


Outputs: "ListBox1 Value: <blank>"
Then: "ListBox2 Value: 0.5 oz"

bludragonner
07-06-2016, 02:23 PM
Actually... now what's weird is that every OTHER time I run the script it works. But the first and every odd number of times running it, it doesn't work as expected.

bludragonner
07-06-2016, 02:34 PM
Sorry guys, That was REALLY weird actions coming from word VBA... A restart got rid of the ghost in the machine I was having. Now it consistently outputs the desired (and expected) values.

gmaxey
07-06-2016, 03:08 PM
Yes there is certainly some weirdness going on here. .Value returns the value of the .BoundColumn for the "selected" item. If you physically select the value in ListBox2 it works every time. Ohterwise it displays the correct value but returns nothing every other time. I have no idea why ListBox1 works every time.

You can get the correct return every time using


Private Sub UserForm_Initialize()
With ListBox1
.AddItem "0.5 oz", 0
.AddItem "1.0 oz", 0
.ListIndex = 1
End With
With ListBox2
.AddItem "0.5 oz", 0
.AddItem "1.0 oz", 0
.ListIndex = 0
End With
End Sub

Private Sub CommandButton1_Click()
MsgBox ListBox2.Value 'Returns the value every other time. Wierd!!
MsgBox "ListBox1 Value: " & ListBox1.List(ListBox1.ListIndex)
MsgBox "ListBox2 Value: " & ListBox2.List(ListBox2.ListIndex)
End Sub

gmayor
07-06-2016, 09:23 PM
Or you could use

Private Sub CommandButton1_Click()
MsgBox ListBox2.Column(0) 'Returns the value every time
MsgBox "ListBox1 Value: " & ListBox1.List(ListBox1.ListIndex)
MsgBox "ListBox2 Value: " & ListBox2.List(ListBox2.ListIndex)
Unload Me
End Sub

gmaxey
07-07-2016, 03:54 AM
Graham,

Any idea why this happens in the first place and why to only listbox2?

gmayor
07-07-2016, 04:12 AM
Regrettably no, but I never cease to be astonished by the occasional weirdness of Microsoft Word. :)

gmaxey
07-07-2016, 04:29 AM
Yes, I too am often in the astonished camp.