2

trying to call this subroutine using a string. I have tried Application.Run like I have read online but that doesn't seem to be working. The variable element will loop through and represents different state codes. So I have subs called "CA_Config", "GA_Config" "AZ_Config" etc.

Dim strSubToCall As String
strSubToCall = element & "_Config()"
Application.Run strSubToCall

State subs are very different therefore need to be different subroutines. The other subs and the main sub calling the other ones are all public.

Example for CA sub below

Public Sub CA_Config()

Dim rngLastHeader As Range
Dim intLastRow As Integer
Dim i As Integer
intLastRow = Sheet1.currWS.UsedRange.Rows.Count
Set rngLastHeader = Sheet1.currWS.Range("A1").End(xlToRight)

rngLastHeader.Offset(, 1).Value = "Use Tax Reversal Needed"

Sheet1.currWS.Range("X:X").EntireColumn.Copy
Sheet1.currWS.Range("Y:Y").PasteSpecial xlPasteFormats
Sheet1.currWS.Range("Y:Y").Columns.AutoFit

End Sub
13
  • 1
    Remove parenthesis () Commented Dec 15, 2017 at 17:57
  • 1
    Perhaps, the Sub is in another module and Private? Commented Dec 15, 2017 at 17:58
  • 1
    Define "doesn't seem to be working" Commented Dec 15, 2017 at 17:59
  • 1
    Instead of having different subs for each state, have one sub with a parameter.... ie., Sub StateConfig("CA") How different are each of these "state subs"? Commented Dec 15, 2017 at 17:59
  • 1
    Prepend your Sub with module name. For instance, Application.Run "Module1.MySub" Commented Dec 15, 2017 at 18:00

1 Answer 1

4

Remove parenthesis and prepend your Sub name with module name. For instance, Application.Run "Module1.MySub".

Sign up to request clarification or add additional context in comments.

10 Comments

@Mat'sMug Please, follow comments under the question - the parenthesis were the first thing I mentioned.
Indeed, looks like a bug. Not in the VBE though - Application.Run is entirely Excel's object model. So, it's a bug in Excel, not in the VBE. But if you tell Microsoft I bet they'll call it a feature ;-)
Another interesting bit: Public Sub Sub1234() is listed in Excel's "macros" list, but the macro dialog's "Run" button is disabled when it's selected, which confirms my long-standing suspicion that Excel's "Run Macro" tool uses Application.Run under the hood. Yet another good reason to use meaningful names!
Sub1 through Sub1048576 won't work, but use a numbering scheme above 2^20, and Sub1048577 will work just fine. It's obvious, right?
@MathieuGuindon Oh wait, duh, "Sub1" is a valid cell address, where the Macro argument to Application.Run can be a Range (presumably to facilitate Excel4 Macros?), and (according the help) when that argument is a string, it is in the context of the active sheet. Yet another instance of name clashes once Excel bumped the row/column counts in 2007. There are a bunch of new inspections for Rubberduck VBA to handle ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.