Macro doesn't work with arguments

  • Thread starter Thread starter Derek Gadd
  • Start date Start date
D

Derek Gadd

I had a macro that worked fine but now when I try to run it (e.g. by
pressing F5 in the code) a dialog box comes up asking me to choose the
macro I want to run. My macro is not there and I can't get it to run.
I've tried creating a new workbook and typing a new macro. But anytime
that I use arguments, it doesn't recognise that the macro exists. For
example:

This will run:
Sub EmailReport()

But this will not:
Sub EmailReport(Optional Monthly As Boolean)

Any suggestions would be appreciated.

Thanks,
Derek
 
You can't run a macro that needs an argument by using F5 or from the macro
list.
 
You can only run a macro directly if it has no arguments.

Try creating a sub like one of those below and use it to call your code.

Sub doEmailReport()
EmailReport
End Sub

Sub askEmailReport()
EmailReport MsgBox("Monthly ?", vbYesNo) = vbYes
End Sub
 
Sorry, the askEmailReport I posted first was probably confusing.
Here's a slightly more comprehensible version:

Sub askEmailReport()
Dim doMonth As Boolean
If MsgBox("Monthly ?", vbYesNo) = vbYes Then
doMonth = True
Else
doMonth = False
End If
EmailReport doMonth
End Sub

This and the doEmailReport sub will both work with sub like the following:

Sub EmailReport(Optional Monthly As Boolean)
MsgBox Monthly
End Sub
 
It is incorrect that you can't run the macro from the dialog box. It
doesn't appear in the list, but you can type in the name of the macro and
then click run and it will run fine (as you show it with an optional
argument). You can't pass an argument in in this manner - at least I don't
know of a way to do it - but in your case, the argument is optional.

The reason it doesn't appear in the list is because of the argument.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top