How to pass ListBox into a Sub?

R

RADO

Hi all,

I am trying to pass a Listbox into a sub, and declared something like this:

Sub Handle_ListBox (MyListBox as ListBox)
MyListBox.Enabled=False
....
End sub

When I try to pass a listbox to the sub:

Handle_Listbox formHello.listboxVendors

I get a "type mismatch" error. The same approach works perfectly for combo
box.

What's the difference? Where am I wrong?

Thanks -
RADO
 
C

Chip Pearson

Rado,

The reason is the both Excel and MSForms define an object with the name
'ListBox'. The compiler uses the one defined in Excel, because Excel's type
library has a higher precedence that MSForms. The solution is to qualify the
object type name with the MSForm library. E.g.,

Sub Handle_ListBox (MyListBox as MSForms.ListBox)

The reason that it works for Comboboxes is that Excel doesn't have an object
named "
"Combobox". (Comboboxes on Excel sheets, from the Forms tool bar are called
DropDowns.)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top