Availability of a procedure for use by multiple userforms

Magic Polygon

New Member
Joined
Aug 20, 2023
Messages
30
Office Version
  1. 2019
Platform
  1. Windows
For multiple userforms, to call a procedure for the first time within each userform, I would prefer not to redeclare the procedure in those userforms. What ways can I avoid this?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Put the procedure in a standard module (e.g., Module1) as Public and call that from the UserForm code.

Can't give you a more detailed response without a more detailed description of what you are doing, but hopefully that gets you going.
 
Upvote 1
I suspect it will solve my problem. Moving the procedure (pasted below) into a standard module has introduced an "Invalid use of Me Keyword" error message. I have to find a way to dynamically reference each userform that calls the procedure that I moved, and put that reference into the procedure's code.

VBA Code:
'creates the caledar for the month / year
Public Sub BuildCalendar(Optional iYear As Integer, Optional iMonth As Integer)

    .
    .
    .
    
    'set the month and year in the calendar
    Me.Controls("MonthLabel").Caption = VBA.MonthName(iMonth, True)
    Me.Controls("YearLabel").Caption = iYear
    
    .
    .
    .

End Sub
 
Upvote 0
Add a new mandatory first argument to the BuildCalendar SUB for passing a pointer to the calling UserForm as follows:
VBA Code:
Public Sub BuildCalendar(ByVal UForm As Object, Optional iYear As Integer, Optional iMonth As Integer)
    'set the month and year in the calendar
    UForm.Controls("MonthLabel").Caption = VBA.MonthName(iMonth, True)
    UForm.Controls("YearLabel").Caption = iYear
End Sub

and then call the Public SUB from each UserForm as follows :
VBA Code:
Call BuildCalendar(UForm:=Me, iYear:=2023, iMonth:=2)
 
Last edited:
Upvote 2
Put the procedure in a standard module (e.g., Module1) as Public and call that from the UserForm code.

Can't give you a more detailed response without a more detailed description of what you are doing, but hopefully that gets you going.
Thanks, I've applied this to my code, I'll know if it helps once I fix everything surrounding it.
I have a further related question. I have two userforms named AppointmentEntryUserForm and BrowseRecordsUserForm. They each have a calendar named DatePickerFrame, which are both the same, and are composed of many controls. Each control has an event-handling procedure found in their respective userform, defined like so:

VBA Code:
Private Sub Day1_Click(): DayClick (1): End Sub
Private Sub Day2_Click(): DayClick (2): End Sub
Private Sub Day3_Click(): DayClick (3): End Sub
Private Sub Day4_Click(): DayClick (4): End Sub
.
.
.

Again. I'd rather not unnecessarily duplicate declarations. Could I simply apply your above solution in this case, by changing Private to Public, and moving these to Workbook1, or would I need to add a change?
 
Upvote 0
You could pass a pointer to each control as an argument in the gerneric public SUB in the same manner.
 
Upvote 0
You could pass a pointer to each control as an argument in the gerneric public SUB in the same manner.
I'm unsure of how to handle events outside of using the drop-down menus to create procedures, as I haven't found a good place to learn all the fundamentals. My guess would be to insert what follows into Module 1:

VBA Code:
Public Sub DayClick(ByVal UForm As Object, DayLabel As Object, i As Integer)

    If UForm.Controls("day" & i).Click Then
        'The below two lines basically defined the DayClick Sub that was called in each Day_Click declaration in my previous reply
        DateControl.Text = UForm.Controls("day" & i).Tag
        DatePickerFrame.Visible = False
    End If

End Sub
 
Upvote 0
I'm unsure of how to handle events outside of using the drop-down menus to create procedures, as I haven't found a good place to learn all the fundamentals. My guess would be to insert what follows into Module 1:

VBA Code:
Public Sub DayClick(ByVal UForm As Object, DayLabel As Object, i As Integer)

    If UForm.Controls("day" & i).Click Then
        'The below two lines basically defined the DayClick Sub that was called in each Day_Click declaration in my previous reply
        DateControl.Text = UForm.Controls("day" & i).Tag
        DatePickerFrame.Visible = False
    End If

End Sub
Or if not this, could I do the following:

  1. Double click a userform control to create a blank event handler for that control.
  2. Cut and paste the event handler into a module.
  3. Change it from private to public?
Again, the context is that I have multiple userforms, each containing a copy of a control. The controls have the same name. I want each control to do the same thing when clicked. I have learned here how to make procedures applicable to multiple userforms. I don't know if it applies to event-handling procedures of the form [name]_[event], like "Day1_Click()". If not, is there way of event handling outside using the dropdown menu in the VBE? Something like a keyword, or method, or anything that I can write explicitly within a procedure, similar to HTML events?
 
Upvote 0
It may be beneficial to look at your total system design, instead of drilling down to a "how do I do this one thing" kind of a question. For example, would it makes sense to have one userform instead of multiple?
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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