ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Return codes from called macros (https://www.excelbanter.com/excel-programming/366334-return-codes-called-macros.html)

mwc0914[_19_]

Return codes from called macros
 

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

--
mwc091
-----------------------------------------------------------------------
mwc0914's Profile: http://www.excelforum.com/member.php...fo&userid=2413
View this thread: http://www.excelforum.com/showthread.php?threadid=55857


Bob Phillips

Return codes from called macros
 
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)

"mwc0914" wrote in
message ...

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

Within the called macro I attempt to have the user open a file so it
can be processed. If the user selects cancel when prompted & doesn't
open the file, I exit the called macro. But now I want my main macro to
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?


--
mwc0914
------------------------------------------------------------------------
mwc0914's Profile:

http://www.excelforum.com/member.php...o&userid=24130
View this thread: http://www.excelforum.com/showthread...hreadid=558576




Tom Hutchins

Return codes from called macros
 

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
------------------------------
"mwc0914" wrote:


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

Within the called macro I attempt to have the user open a file so it
can be processed. If the user selects cancel when prompted & doesn't
open the file, I exit the called macro. But now I want my main macro to
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?


--
mwc0914
------------------------------------------------------------------------
mwc0914's Profile: http://www.excelforum.com/member.php...o&userid=24130
View this thread: http://www.excelforum.com/showthread...hreadid=558576



colofnature[_65_]

Return codes from called macros
 

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

--
colofnatur
-----------------------------------------------------------------------
colofnature's Profile: http://www.excelforum.com/member.php...fo&userid=3435
View this thread: http://www.excelforum.com/showthread.php?threadid=55857



All times are GMT +1. The time now is 04:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com