Easy way to check if a macro exists, or ignore the call if it doesn't?

Jorgen

Board Regular
Joined
Mar 25, 2004
Messages
67
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hi,
I am looking for an easy way to check if a macro exists so it doesn't create an error if it isn't there or just ignores the call. The "main" macro is in a workbook that works with a lot of different workbooks, some that contain the "MyMacro" and some don't. And if it exists it should be run...

I tried

On Error Resume Next
Call ActiveWorkbook.Sheets("StartSheet").MyMacro

And if it doesn't exist, I end up with:
"Run-time error '438':

Object doesn't support this property or method"

Any ideas that doesn't require scanning through all macros of the workbook? It needs to be run without having programmatic access to the VBA project (like in http://www.cpearson.com/excel/vbe.aspx)

Thanks!

Jörgen
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
It would have been a lot easier if the macros were in normal modules where they belong and not in worksheet modules. As long as they are not declared as private, you could use:
Code:
   On Error Resume Next
   CallByName ActiveWorkbook.Sheets("StartSheet"), "MyMacro", VbMethod
 
Upvote 0
Hi Rory,
Thank you for the suggestion, but I still get the same error 438 if the macro "MyMacro" doesn't exist in the StartSheet-VBAcode...

Thanks,
Jörgen
 
Upvote 0
Then I'd have to guess you have 'Break On All Errors' set on the General tab under Tools-Options in the VBEditor.
 
Upvote 0
It's like a bus - you don't use it for ages, then suddenly all these questions come along at once! :)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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