How to call a sub from a sheet with arguments?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
Office Version
  1. 365
Platform
  1. Windows
I have a Sub that does a lot of analysis on one named range and returns the results to another named range. I call it using a button control. The problem is if I want to have it work on more than 1 range, I have to keep renaming the ranges. I would like to be able to have a different button control for each range, on the same sheet or a different one in the same book. Each button would call the same Sub, but with a different range.

I tried adding parameters to the Sub statement (Sub MySub(Name1 as range)), but then it no longer appears in the list of macros to assign to the button.

Is there a way to do this?

Thanks
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Create parameterless macros:

VBA Code:
Public Sub InvokeOne()
    Dim One As Range
    mySub One
End Sub

Public Sub InvokeTwo()
    Dim Two As Range
    mySub Two
End Sub
 
Upvote 0
Following on from @GWteB's comment. Paul Kelly from Excel Macro Mastery normally has a module called Click_Events and he puts all his initiating macros there.
eg
Module > Click_Events
Contains all the Click Macros eg Public Sub GenerateReports_Click()​
and all that macro does is
Call MySub (rng) - the macro that actually does the work.​
 
Upvote 0
Create parameterless macros:

VBA Code:
Public Sub InvokeOne()
    Dim One As Range
    mySub One
End Sub

Public Sub InvokeTwo()
    Dim Two As Range
    mySub Two
End Sub
I thought of that, but that would be a bit inconvenient if I needed more than a few different calls. I was looking for a more general solution.
 
Upvote 0
Following on from @GWteB's comment. Paul Kelly from Excel Macro Mastery normally has a module called Click_Events and he puts all his initiating macros there.
eg
Module > Click_Events
Contains all the Click Macros eg Public Sub GenerateReports_Click()​
and all that macro does is
Call MySub (rng) - the macro that actually does the work.​
Is that here:

Paul Kelly
 
Upvote 0
I thought about calling the Sub from a UDF. I'd pass the arguments to the UDF and it would pass them to the Sub. But the UDF got an error message (I can't recall which).
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,559
Members
448,970
Latest member
kennimack

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