Passing a Form into a function

romanemul

New Member
Joined
Jul 22, 2016
Messages
35
Hello dear forum users,

Im making a application where i have couple of listboxes on a form. The problem is that my code is repeating too much so im trying to avoid this repetitive code.

Code:
for i = 1 to listbox.ListCount......

boilerplate code to find selected index

So i decided to make a function with the form as an input and an output of an selected index.

Code:
Private Function selectedIndex(ByVal lstbox As MSForms.ListBox) As Integer

for i = 1 to lstbox.ListCount......

so im running the function like this

Code:
idx = selectedIndex(listbox)


The problem is that when i pass a name of a form it pass only bounded value. I even tried to pass a form as an object but wihout luck.


Can anyone provide me a hint how to pass an form ?

Thanks in advance.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Your function takes a listbox, not a form, so I don't understand your question. Can you clarify?
 
Upvote 0
Sorry my bad .

It supposed to be

"So i decided to make a function with the listbox as an input and an output of an selected index."

and

"The problem is that when i pass a name of a form it pass only bounded value. I even tried to pass a form as an object but wihout luck."

supposed to be

"The problem is that when i pass a name of a listbox it pass only bounded value. I even tried to pass a listbox as an object but wihout luck."

and the topic name is also bad


supposed to be "Passing a listbox into a function"

Sorry
 
Upvote 0
What makes you think it’s only passing the value of the list box?
 
Upvote 0
I wanted just to pass a just a pointer of a listbox to function which is faster than passing the whole form.

but hey. I have made few changes in my code and it returns me correct index and previously it returns me just -1.

Completely stupid and bogus question.

Thanks anyway Rory


Rom
 
Upvote 0

Forum statistics

Threads
1,224,743
Messages
6,180,686
Members
452,994
Latest member
Janick

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