Return codes from called macros

  • Thread starter Thread starter mwc0914
  • Start date Start date
M

mwc0914

I have my main macro that calls another macro via Application.Ru
"Called Macro".

Within the called macro I attempt to have the user open a file so i
can be processed. If the user selects cancel when prompted & doesn'
open the file, I exit the called macro. But now I want my main macro t
know that this just happened so I can exit it also.

How do I pass return codes back from a called macro to the main macro
reference the return code in the main macro
 
Make the macro a function, and return a status code from that function. To
call it, use

rtn = Application.Run("'wkb'!myFunc")

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
Set up your called macro as a function. The example below calls a function
named 'CalledMacro', which returns either a string containing the selected
filename or a null string (if the user canceled out of the File Open dialog).

Sub TEST()
Dim FileToOpen As String
FileToOpen$ = CalledMacro
If FileToOpen$ = vbNullString Then Exit Sub
'Otherwise, open the selected workbook.
Workbooks.Open Filename:=FileToOpen$
End Sub

Function CalledMacro() As String
Dim Fyle As String
Fyle1$ = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
If Fyle1$ = "False" Then
CalledMacro = vbNullString
Else
CalledMacro = Fyle1$
Exit Function
End Function

Hope this helps,

Hutch
 
Change your sub "Called Macro" to a function, then you can pass bac
results like in this example where main_macro calls called_macro an
receives the return value into a variable:

sub main_macro
dim TheResponse as string
TheResponse = called_macro()
msgbox TheResponse
end sub

function called_macro() as string
dim Answer as integer
Answer = msgbox("Yes or No?", vbyesno)
if Answer = vbyes then
called_macro = "Yes"
else
called_macro = "No"
endif
end function


Co
 

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