object required error using listbox

Reset

Board Regular
Joined
Apr 16, 2010
Messages
227
I am trying to place selected items from a listbox (form control) into an array. I know just enough to be dangerous. The follownig code was lifted directly form a previous posting (ref Andrew Poulsom):

Sub test()

Dim i As Integer
Dim Msg As String
For i = 0 To ListBox7.ListCount - 1
If ListBox7.Selected(i) Then
Msg = Msg & ListBox7.List(i) & vbCrLf
End If
Next i
MsgBox "Selected items are:" & vbCrLf & Msg
End Sub

The only change is it is ListBox7, because I keep trying to redo this. when insert a listbox and go to assign it to a macro it auto fills the macro name as ListBox#_Change. So I am assuming the listbox would be named as such in any code. However, when i run the above code I get "Run-time error '424': Object required".

So, what am I doing wrong? The ultimate goal is to add selected items into an array. Thanks.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Please use code tags.

Since you did not put that code in a userform's object standard event, you have to prefix the control's name with the userform's object name, Userform1, typically.

For the array part, use Split().
 
Upvote 0
If you are using a form control, then try:
Code:
Sub test()

   Dim i                 As Integer
   Dim Msg               As String
   Dim LB                As ListBox
   Set LB = ActiveSheet.ListBoxes(Application.Caller)
   For i = 1 To LB.ListCount
      If LB.Selected(i) Then
         Msg = Msg & LB.List(i) & vbCrLf
      End If
   Next i
   MsgBox "Selected items are:" & vbCrLf & Msg
End Sub
 
Last edited:
Upvote 0
Rory,

With your code provided I have two problems. First my input range is $C:$C, so as is I get an overflow error as the box is including the thousands of blank cells below the last filled cell.

The other problem is that it runs as soon as I choose one entry. I need to be able to make multiple selections then run the code using a button.
 
Upvote 0
Then you'll need to declare i as Long, assign the macro to a button, and refer to the listbox by name, rather than using Application.Caller.
I also would recommend assigning just the data range, not the entire column.
 
Upvote 0
Thanks for the input. I went in another direction and got it to work.

For the comment "please use code tags", I don't know how to do that. Tried looking around for instructions but didn't find any.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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