Listbox Control as Parameter

vthokienj

Board Regular
Joined
Aug 1, 2011
Messages
105
Office Version
  1. 365
Platform
  1. Windows
This seemingly simple piece of code is giving the 'run time error 424 object required' error. How do you pass an activeX listbox as a parameter to a module?

Code:
Public Sub btnAddToListbox_Click()
listboxSystemSelection.AddItem "item added"
    DisplayItemsInListbox (listboxSystemSelection) ' crashes here
End Sub

Code:
Public Sub DisplayItemsInListbox(ByRef thelistbox As ListBox)
    thelistbox.AddItem ("added this")
End Sub


I've tried ByVal, ByRef, neither, and MsForms.ListBox.
Thanks for any help.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This works ok for me:

Code:
Private Sub CommandButton1_Click()
    ListBox1.AddItem "Item 1"
    
    CallList ListBox1
End Sub

Private Sub CallList(ByVal lbx As MSForms.ListBox)
    lbx.AddItem "Item 2"
End Sub
 
Upvote 0
You just need to remove the parentheses around the object you are passing:
Code:
DisplayItemsInListbox listboxSystemSelection
or use Call:
Code:
Call DisplayItemsInListbox(listboxSystemSelection)
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,508
Members
452,918
Latest member
Davion615

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