Problem with ListBox

sbe70

New Member
Joined
Jul 9, 2011
Messages
11
Hi,
I am clearly not understanding how one declaring and using listboxes in Excel. So a novice needs some help here.

I am trying to create a ListBox, not ActiveX, where I have a list of items in the box. I would like to select several items and via a vba macro identify wihich one I have selected. I have created a ListBox (Form Controls) and populated the box with Worksheets("Menu").Shapes("FirstBox").ControlFormat.AddItem Value. I have named the listbox "FirstBox" and it is placed in worksheet "Menu". So good so far. Now I select a few items in the list. I would like to connect a macro that reads the selected items from the listbox. The code I am using is as follows
Sub FirstBox_Change()
Dim firstlist() As Variant
Dim FirstBox As Shape
Dim antal As Integer
Sheets("Menu").Select
selCount = -1
antal = FirstBox.ListCount
Sheets("Menu").Select
For i = 0 To FirstBox.ListCount - 1
If FirstBox.Selected(i) = True Then
selCount = selCount + 1
ReDim Preserve firstlist(selCount)
firstlist(selCount) = FirstBox.List(i)
End If
Next
End Sub

When I select an item in the listbox I get the following error message
"Run Time Error 91", "Object variable or With block variable not set"

I assume there is something wrong in my declarations.

What is missing or what am I doing wrong?

Many thanks
Stefan
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Using a forms list box (which does not have a multiselect option) you would probably have to do it something like below.
If you declare your array and its count "c" at the top of your code window they will hold there values throughout your selections.
As you will see I have set the number of selections to 3, alter to suit !!!
Code:
Option Explicit
Dim firstlist() As String
Dim c
Sub FirstBox_Change()
Dim i As Long
With ActiveSheet.Shapes(Application.Caller).OLEFormat.Object
For i = 1 To .ListCount
   If .Selected(i) Then
        c = c + 1
        ReDim Preserve firstlist(c)
        firstlist(c) = .List(i)
    End If
Next
End With
If c = 3 Then
MsgBox Mid(Join(firstlist, ","), 2)
Erase firstlist
c = 0
End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top