0

If I have a Sub that has 2 Optional arguments. Can I run that Sub directly without having to use the Call Statement from another Sub?

Basically, I created a Sub that builds a report from other workbooks using yesterday's date automatically (*the date is found in the other workbook's filenames that I pull the data from). But, I wanted to possibly give the option to be able to prompt the user for a Date to run the report for. The reason for the extra sub that could be run separately to prompt for input is because I plan to try and automate the main Sub/Macro to run using a Script and Task Scheduler.

I noticed if I modify my main sub that builds the report and add optional arguments to the sub BuildReport(....) line, I cannot directly run that sub any longer. Remove the args and I can run it again.

I thought if the args were set to "Optional" args I'd be able to run it still without using a "Call" from another Sub.

Is this correct that a sub with ANY args, optional or not, cannot be run directly?

Matt
  • 11
  • 3
  • If by "directly run" you mean from the Alt+F8 list of macros, then adding optional parameters to a sub should not prevent it from appearing in the list. – Tim Williams Sep 10 '20 at 23:37
  • I just created 2 test Subs. Sub1 has 2 optional arguments, and Sub2 does 2 input boxes and captures the user input, which then calls Sub1 and passes the 2 captured values. I am able to both Run and Step Into Sub2. However, I can neither Run nor Step Into Sub1. Pressing Alt+F8 also does not show Sub1 as an option to be run. – Matt Sep 11 '20 at 16:51
  • Sorry I only tested with optional variants eg. `Optional arg1` - as soon as I added a specific type `Optional arg1 as String` it no longer shows in the list – Tim Williams Sep 11 '20 at 17:05

0 Answers0