Run macro by name

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,836
Office Version
  1. 2010
Platform
  1. Windows
I have a workbook where various sheets get published based on various criteria. For each sheet to be published, I want to run a short macro (that takes no arguments) behind each sheet. Depending on the sheet, it may autofilter a table, or set the zoom, or do nothing at all. This doesn't work:

VBA Code:
Sub x()
  Dim wks As Worksheet
 
  For Each wks In Worksheets
    If isGonnaGetPublished(wks) Then
      Application.Run wks.CodeName & ".BeforePublish"
    End If
  Next wks
   
    ' other stuff
End Sub

What have I forgotten?

Thanks in advance.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Does each worksheet have the same BeforePublish function? If it's named using the standard convention of [WorsheetName]_[FunctionName]([Args]) If so, perhaps this would work:

VBA Code:
Sub x()
    Dim wks As Worksheet
 
    For Each wks In Worksheets
        If isGonnaGetPublished(wks) Then
            Call wks.Name & "_BeforePublish"
        End If
    Next wks
  
    ' other stuff
End Sub
 
  • Like
Reactions: shg
Upvote 0
I have a workbook where various sheets get published based on various criteria. For each sheet to be published, I want to run a short macro (that takes no arguments) behind each sheet. Depending on the sheet, it may autofilter a table, or set the zoom, or do nothing at all. This doesn't work:

VBA Code:
Sub x()
  Dim wks As Worksheet
 
  For Each wks In Worksheets
    If isGonnaGetPublished(wks) Then
      Application.Run wks.CodeName & ".BeforePublish"
    End If
  Next wks
 
    ' other stuff
End Sub

What have I forgotten?

Thanks in advance.

Are the BeforePublish macros located in their corresponding worksheet modules respectively ? If so, as far as I can tell, your code should work.

Have you considered using the vba interaction CallByName Method as follows :
VBA Code:
 For Each wks In Worksheets
    If isGonnaGetPublished(wks) Then
        CallByName wks, "BeforePublish", VbMethod
    End If
 Next wks
 
  • Like
Reactions: shg
Upvote 0
Solution
Does each worksheet have the same BeforePublish function?
Richh, thanks for responding.

No, each (publishable) worksheet has its own, some of which do nothing, and their names are all identical. Here's one
VBA Code:
Sub BeforePublish()
  With Me
    .AutoFilterMode = False
    .Range("tbl").CurrentRegion.AutoFilter Field:=2, Criteria1:="<>"
    .PageSetup.Zoom = 122
  End With
End Sub
Have you considered using the vba interaction CallByName Method ...:
That's perfect, Jaafar, thank you.
 
Upvote 0
Well that's weird; I reran it so I could post the error message, and now it works fine. I think that's debugging by intimidation ("Don't make me tell Rory you don't work!)".

Thanks for weighing in.
 
Last edited:
Upvote 0
:)
My colleagues frequently complain that their computers always work perfectly when I stand next to them so they can show me what’s wrong…
 
Upvote 0
At random times, the same problem recurs. The message says the macro may not exist, or macros are disabled. When that happens, CallByName also fails (different message, did'nt write it down).

Restart Excel, problem goes away.

Rory, can you come stand by my 'puter pro re nata?
 
Upvote 0

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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