Question

Could someone with a better level of excel capability please assist me with the below code? I have set this up in the Worksheet_Activate event. I have code to set an ActiveX listbox to a default value, as shown below. For whatever reason, the listbox is not showing the default value as a highlighted item. All the other logic seems to work fine but it's driving me crazy that the below code won't highlight the stupid first item in my first listbox. What am I doing wrong?

With CTOverview.ListBox1
    .IntegralHeight = True
    .Height = 114.75
    .Width = 125.25
    .IntegralHeight = False
    .ListIndex = 0
    .Selected(0) = True
    .Value = "Entire Division"
End With

CTData.Range("Overview_RegionSelected").Value = CTOverview.ListBox1.Value

With CTOverview.ListBox2
    .IntegralHeight = True
    .Height = 114.75
    .Width = 150
    .IntegralHeight = False
    .ListIndex = -1
End With

Thanks for any help.

Was it helpful?

Solution 2

I know. I have seen this weird behavior in the past with absolutely no explanation. Sometime it works and sometimes it doesn't. Try this. This will work.

With CTOverview.ListBox1
    If .ListCount > 1 Then .Selected(1) = True
    .IntegralHeight = True
    .Height = 114.75
    .Width = 125.25
    .IntegralHeight = False
    .ListIndex = 0
    .Selected(0) = True
    .Value = "Entire Division"
End With

FOLLOWUP

Here is a better code than the code that I gave you earlier. The above code was restricted to the fact that we needed to have more than 1 Listcount. The below will work for 1 Listcount as well.

Dim rng As Range, aCell As Range

With CTOverview.ListBox1
    .IntegralHeight = True
    .Height = 114.75
    .Width = 125.25
    .IntegralHeight = False
    .ListIndex = 0
    .Selected(0) = True
    .Value = "Entire Division"

    Set rng = Range(.ListFillRange)
    For Each aCell In rng
        aCell.Formula = aCell.Formula
    Next
End With

OTHER TIPS

Setting .Selected to False and immediately setting it to True worked for me:

With Me.Charts_LB
    .ColumnHeads = False
    .RowSource = rng.Address
    .Height = 480
    .Selected(Charts_ListIndex) = False
    .Selected(Charts_ListIndex) = True
End With

Simplify your code. Either use the .Selected property or the .ListIndex property or the .Value property to have an item in the list selected.

In your first listbox (ListBox1), you have used all the three properties to have your default selection in effect. Probably you are having inconsistent values in your code like the first item of the list not being "Entire Division". If your first item is "Entire Division" only, then have any one of the properties in place to effect your desired selection.

In the second listbox (ListBox2), when your code has .ListIndex = -1, it means that you want to deselect everything. So change the desired list index to 0 to have the first item selected.

Given that you want the first options of the list boxes selected by default, have the following as a base.

With CTOverview.ListBox1
    .ListIndex = 0
End With

With CTOverview.ListBox2
    .ListIndex = 0
End With

Let me know your thoughts.

So I had the same problem and I referred the Office documentation and realized that everything depended on the premise that the listbox should be in focus to have its value attribute to return value.

I propose that you simply add a line at the end to bring focus.

With CTOverview.ListBox1
    .IntegralHeight = True
    .Height = 114.75
    .Width = 125.25
    .IntegralHeight = False
    .ListIndex = 0
    .Selected(0) = True
    .Value = "Entire Division"
    .SetFocus
End With

I have seen a similar problem with ListBox controls in UserForms (not ActiveX controls embedded in a worksheet), but since I haven't found any other postings that show up on the first couple of Google search pages, I'm attaching my solution to this one, since it is likely that people hunting for UserForm solutions will hit on this post, plus it may also be helpful to some who are using straight ActiveX ListBox controls.

In UserForms (at least) setting .ListIndex = n and .Selected(n) = True doesn't seem to help at all. The only workaround that I've found is this, 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top