Repeating code

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,257
I have this code in two separate userforms but wondered if there is a way I can only write it once somewhere and refer to it.

Code:
Dim MyArray() As Variant
    
    MyArray = Array(1, 2, 3, 4, 5)
    
    Me.LsitBox1.List = MyArray
    
    Erase MyArray)
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,900
Put the code in a module (not the form), but call it from the form

When you record a macro , it will create a module for you.
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,257
Put the code in a module (not the form), but call it from the form

When you record a macro , it will create a module for you.

I suppose I would have to have a parameter in the module to determine which userforms it's coming from.
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Something like this:- in your "Userfrom modules"
Code:
Private Sub UserForm_Initialize()
Call Lb(ListBox1)
End Sub

and this in you ordinary module:-
Code:
Sub Lb(List_Obj)
List_Obj.List = Array(1, 2, 3, 4, 5)
End Sub
Regrds Mick
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,334
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

Something like this:- in your "Userfrom modules"
Code:
Private Sub UserForm_Initialize()
Call Lb(ListBox1)
End Sub

and this in you ordinary module:-
Code:
Sub Lb(List_Obj)
List_Obj.List = Array(1, 2, 3, 4, 5)
End Sub
Regrds Mick

I don't know if the OP would need the flexibility to change the number to "count to" for multiple ListBoxes, but if so,your approach can be made flexible to allow for that. Just change the LB subroutine to this...

Code:
Sub LB(List_Obj As ComboBox, CountTo As Long)
  List_Obj.List = Evaluate("TRANSPOSE(ROW(1:" & CountTo & "))")
End Sub

And then call it like this...

Code:
 Private Sub UserForm_Initialize()
  Call LB(ListBox1, 5)
  Call LB(ListBox2, 9)
 End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,109,436
Messages
5,528,758
Members
409,834
Latest member
vexceled

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top