Quicky: How do I pass Listbox address to a universal sub routine?

Julesdude

Board Regular
Joined
Jan 24, 2010
Messages
197
This might well be a no brainer, so sorry about that. I wanted to tidy up my code and instead of repeating it x 3, to have just one set of code with the relevant reference information passed to it.

For example, I have 3 listboxes on a userform with 3 accompanying buttons.
When one of the buttons is clicked it deletes what has been selected in the corresponding listbox as well as checking to see if the entry is in the other 2 listboxes, deleting there too if it is
.
But what that means is I repeat the code that does this for each command button sub relating to each listbox.

What I'd prefer to do (and hopefully someone can help me), is call to a sub procedure the listbox reference I wish to delete the entry in.

How do I do that? I've already tried passing to a sub procedure using:

call listboxdelete(listbox1, listbox2, listbox3)

...and have the procedure start:

Private Sub listboxdelete(sourceLB as listbox, otherLB1 as listbox, otherLB2 as listbox)

But this is giving me a type mismatch error!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You might get rid of the error by using MSForms.Listbox but it really isn't clear what you actually want to do.

As diddi says, post the code.

Perhaps a short explanation of what you are trying to do as well.:)
 
Upvote 0
Something like this , Perhaps:-
Code:
Private Sub CommandButton1_Click()
Call nlist(ListBox1)
End Sub
Code:
Sub nlist(lb As MSForms.ListBox)
MsgBox lb.List(1, 0)
End Sub
Mick
 
Upvote 0
Sorry I had to dip away onto another project this week.

I think Mick has the general idea, thanks Mick. But I'll explain again as i wasn't clear enough in first post.

Putting the listboxes I mentioned to the side, say I have a large amount of numeric textboxes on a userform - perhaps 30 - 40. I want to transfer the textbox values to variables so that some complex calculations and measurements can be made from the values entered. To achieve this, instead of using the following code....

Code:
Dim TextBoxVars(1 to 30) 
TextBoxVars(1) = textbox1.value 
TextBoxVars(2) = textbox2.value
TextBoxVars(3) = textbox3.value
' ......etc etc

....which could amount to about 40 lines! Is there a way of looping through textboxes and assigning to the variable array, perhaps using a For - Next loop for example? Can you add a '1', '2', '3', generated by a For variable to the 'textbox' object name?
 
Upvote 0
Yep:
Code:
Dim n as long
Dim TextBoxVars(1 to 30)
for n = 1 to 30
 TextBoxVars(n) = me.controls("textbox" & n).value
next n
 
Upvote 0
Thanks Rory that's superb. As always, anything's possible.

I guess using this method I can actually link variables to a whole host of controls. Good to know and I'll play around a bit to see what I can achieve.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,459
Members
452,915
Latest member
hannnahheileen

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