PDA

View Full Version : Select first Item in Listbox



Djblois
10-12-2007, 12:46 PM
I am trying to select the first item in a list box with its index number. This is what I have tried and it keeps crashing:

Me.lbColumn1SC.ListIndex.Value = 1

The reason why I am trying it this way is because the list is always different so I can't select in by name.

I even tried:

Me.lbColumn1SC.ListIndex = 1

Paul_Hossler
10-12-2007, 04:14 PM
Djblois --

Try



Private Sub UserForm_Initialize()
With Me.ListBox1
.Value = .List(0)
End With
End Sub



Paul

mikerickson
10-12-2007, 04:48 PM
If the list box is on a Userform or from the Toolbox,

Me.ListBox1.Selected(0) = TrueIf its from the Forms menu, its 1 based and this syntax is needed

ActiveSheet.Shapes("List Box 1").OLEFormat.Object.Selected(1) = True

Djblois
10-15-2007, 08:04 AM
This Crashes the program:



Me.ListBox1.Selected(0) = True


and this one won't compile:

With Me.ListBox1
.Value = .List(0)
End With

Norie
10-15-2007, 08:14 AM
Can you tell us where the listbox is actually located?

When are you actually trying to do this?

Is the listbox actually populated when you try it?

How is it 'crashing'?

lucas
10-15-2007, 08:24 AM
Why are you making us guess Daniel? Please provide an example...

Djblois
10-15-2007, 08:48 AM
Sorry,

It is in a form. It is populated already. I add the data to it before I show the form with this:

PivotSummary.lbColumn1SC.List = Array("Product", "Division", "Department", "Country", _
"P-Category", "P-Manager")

However, I am trying to add this in the forms code window:

With Me.ListBox1
.Value = .List(0)
End With

The reason I am trying to add it there is I use the same form for multiple reports. Ex: Customer Reports and Product Reports and even though I use this to chose one of the items in the list before I show the form, nothing is selected:

PivotSummary.lbColumn1SC.Value = "Product"

so I am trying to get it selected other ways.

Norie
10-15-2007, 09:57 AM
Daniel

So how is it 'crashing'?

What error message if any are you getting?

Djblois
10-15-2007, 10:42 AM
"Could not select the selected Property. Invalid property value."

and to double check I bypassed the line and it was populated.

However, if I change the True to False then it doesn't crash. I also tried a value of 1 and it still crashes with the same error message.

Norie
10-15-2007, 10:44 AM
Did you try this?


Me.lbColumn1SC.ListIndex = 0

Djblois
10-15-2007, 11:08 AM
no but I tried

me.lbcolumn1SC.listindex = 1

and that also crashed with a error saying:

Could not select the set the List Index Property. Invalid property value

Norie
10-15-2007, 01:02 PM
Daniel

Is listindex definitely in lower case?

Djblois
10-15-2007, 01:19 PM
No, I just checked it is "ListIndex"

lucas
10-15-2007, 01:27 PM
since you don't seem to be willing to post an example....Paul's method works so you're doing something different which we can't see because......I just don't understand.:dunno :dunno :dunno

Djblois
10-15-2007, 01:33 PM
Lucas,

the underlying code is so large and ingrained in the program it would take me about 3 hours to post an example. I am not trying to make it any harder, just to show what I am doing fully would take a long time.

lucas
10-15-2007, 01:43 PM
I don't know what to say.....did you try the example that I posted with Paul's code? Did it work for you in the example....if so why can't you incorporate it into your code?

jmfurban
12-05-2007, 12:04 PM
Thank you... I needed this!

rory
12-06-2007, 05:14 AM
Daniel,
You do have the

me.lbcolumn1SC.listindex = 0
line after the line that populates the listbox, don't you? (If not, you will get the error you describe)

pstraton
06-09-2017, 12:05 PM
My experience with ListBox controls in UserForms is that setting .ListIndex = n or .Selected(n) = True doesn't seem to help at all. The only workaround that I've found is the following, where "n" is the list item to be selected/highlighted:


Dim SaveVal As String
With MyUserForm.SomeListBox
SaveVal = .List(n)
.Value = ""
.Value = SaveVal
.SetFocus 'Not required, but allows up/down arrow keys to function normally.
End With

Note that just re-assigning the selected list item to its current value doesn't work because the underlying code apparently checks for that case and treats it as a no-op.

SamT
06-09-2017, 01:10 PM
10 yo thread :)