How to call a sub from a sheet with arguments?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
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
 
A UDF is simply not the super glue that can do everything ...

 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
As GWTeB has already confirmed yes that is the Paul Kelly I was referring to.
I tried googling a reference to how he structured his Click_Events but I am afraid I can't find it.
He has lots of stuff on YouTube but he only makes his longer lesson type webinar replays available for 48 hours and that is where you would be able to more of his approach.
(other than buying his handbook or his subscription offering)
Here is one of his shorter ones (9 mins). Which includes using the watch window
 
Upvote 0
A UDF is simply not the super glue that can do everything ...

That's an understatement. But UDFs are a h*ll of a lot more super than macros.

VBA is by far the worst programming language I have ever used and I've used more than a dozen going all the way back to Fortran, COBOL, and BAL. It's even a step down from VB, which is a high (low?) bar. I probably spend about as much time looking for workarounds to the many limitations and inconsistencies in Excel, VBA, Access, PowerPoint, Outlook, and even Word, as I do in productive work. The inability to pass parameters to macros is stupid, stupid, stupid.

That page you referenced from m$ft reminds me of statements from politicians justifying their self-serving behaviors or religious leaders explaining the unexplainable.
 
Upvote 0
I'm afraid you're comparing apples to oranges. We are not talking about programming in its own right. We are talking about an application (Excel) that supports automation through a (derived) programming language. Despite certain limitations, I find VBA quite adequate.

If you really want to get the most out of it: teach yourself Intel assembly language, write your own DLLs and have the time of your life. Everything is possible and it all goes very fast.
 
Upvote 0
As GWTeB has already confirmed yes that is the Paul Kelly I was referring to.
I tried googling a reference to how he structured his Click_Events but I am afraid I can't find it.
He has lots of stuff on YouTube but he only makes his longer lesson type webinar replays available for 48 hours and that is where you would be able to more of his approach.
(other than buying his handbook or his subscription offering)
Here is one of his shorter ones (9 mins). Which includes using the watch window
That is an excellent video. I'll have to look up moce from him. Thanks.
 
Upvote 0
Actually just circling back to this
does a lot of analysis on one named range and returns the results to another named range

Are the named ranges meaningful enough to be usable in a drop down box ?
And ideally is there a naming convention so that the source and destination names are translatable eg same root just a different suffix or prefix ?

If the above is the case you could have a drop down box allowing you to select the range name or range name root.
eg Drop down select = CalculateUK. Translated to range names CalculateUKSource & CalculateUKResult.

Possibly triggered from a Worksheet Change Event OR a button

My (validation list) drop down box was in I3.

VBA Code:
Sub ScenarioRangeSelect()

    Dim rngSrc As Range
    Dim rngResult As Range
    
    Set rngSrc = Range(Range("I3").Value & "Source")
    Set rngResult = Range(Range("I3").Value & "Result")
    ' For testing only
    Debug.Print "Drop Down Value: " & Range("I3").Value
    Debug.Print "Source: " & rngSrc.Address
    Debug.Print "Results: " & rngResult.Address

    ' Call Action sub routine
    Call MakeBold(rngSrc, rngResult)

End Sub


' Action sub routine
Public Sub MakeBold(rngSrc As Range, rngResult As Range)

    rngSrc.Font.Color = vbBlue
    rngResult.Font.Color = vbBlue

End Sub
 
Upvote 0
Actually just circling back to this


Are the named ranges meaningful enough to be usable in a drop down box ?
And ideally is there a naming convention so that the source and destination names are translatable eg same root just a different suffix or prefix ?

If the above is the case you could have a drop down box allowing you to select the range name or range name root.
eg Drop down select = CalculateUK. Translated to range names CalculateUKSource & CalculateUKResult.

Possibly triggered from a Worksheet Change Event OR a button

That's an interesting idea. I haven't done a lot of work with controls, so I'll have to play with it a bit, but thanks for the suggestion. ?
 
Upvote 0
Would it be possible to select the range (or if the columns are fixed, simply any cell on the row) before clicking the button? If so, you could use one macro that uses Selection (if you are selecting the entire range) or the range constructed from the ActiveCell (if the columns are fixed). Alternately, if your columns are fixed, you could designate a cell to type the row number into and create the range inside the macro using that.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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