ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to transfer a ListBox to a function (https://www.excelbanter.com/excel-programming/393470-how-transfer-listbox-function.html)

Stefan Mueller[_2_]

How to transfer a ListBox to a function
 
In VBA I'd like to transfer a ListBox to a function.

For example:
Private Sub CommandButton1_Click()
Call GetIndex(ListBox1)
End Sub

Public Sub GetIndex(TempListBox As ListBox)
MsgBox (TempListBox.ListIndex)
End Sub

Why do I get in VBA (e.g. Excel) at the statement
Call GetIndex(ListBox1)
the Run-time error '13' (Type mismatch)? In real VB it works.

PS: If I use
Public Sub GetIndex(TempListBox As Variant)
it works. But I don't understand why.


Jim Thomlinson

How to transfer a ListBox to a function
 
Give this a whirl...

Private Sub CommandButton1_Click()
Call GetIndex(ListBox1)
End Sub

Public Sub GetIndex(TempListBox As MSForms.ListBox)
MsgBox (TempListBox.ListIndex)
End Sub

In XL there are a couple of different types of listboxes. Unless specified
otherwise VBA assumes you mean the native listbox (the type that comes from
the forms toolbar) not the ActiveX control that you are using in your form
(or at least I think that is the way it works)...
--
HTH...

Jim Thomlinson


"Stefan Mueller" wrote:

In VBA I'd like to transfer a ListBox to a function.

For example:
Private Sub CommandButton1_Click()
Call GetIndex(ListBox1)
End Sub

Public Sub GetIndex(TempListBox As ListBox)
MsgBox (TempListBox.ListIndex)
End Sub

Why do I get in VBA (e.g. Excel) at the statement
Call GetIndex(ListBox1)
the Run-time error '13' (Type mismatch)? In real VB it works.

PS: If I use
Public Sub GetIndex(TempListBox As Variant)
it works. But I don't understand why.



Stefan Mueller[_2_]

How to transfer a ListBox to a function
 
Great, many thanks.



All times are GMT +1. The time now is 07:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com