Macro exits sub after calling macro from another workbook (Application.Run)

fibonacci1101

Board Regular
Joined
May 23, 2013
Messages
83
Hello Everyone,

Here is my code:

Code:
Sub example()

wbName = "exampleWB.xlsm"
sPath = "C:\Users\Username\Documents\"
wbPath = sPath & wbName
mName = "Macro1"

'[1st part of code]

Workbooks.Open (wbPath)

Application.Run ("'" & wbName & "'!" & mName)

'[2nd part of code]

End Sub

When I run this macro it executes 1st part of the code, then it opens exampleWB.xlsm workbook, executes Macro1 but then stops omitting 2nd part of the code. When I do it step by step after executing Application.Run ("'" & wbName & "'!" & mName) line the cursor is just blinking under this line.

Could you help me on this one?
 
@Norie, I figured out that there must be End statement embeded in the protected code. That's why after executing the code macro stops.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Bit strange if there is, especially if the code is being used by other users.
 
Upvote 0
@Norie, speaking about this password protected macro it works just fine - it does what it should. After executing the code it gives msgbox that the procedure has been completed and probably after that msgbox there is End statement. Other users are using this password protected workbook alone so for them it works perfectly. My situation is worse cause I wanted to use this workbook as a part of another macro that I've been creating. But with End statement at the end of this password protected code my code cannot be executed.
 
Upvote 0
What happens when you step through the code with F8?

Also, what does the next part of your code do? ie the code after the call to the macro in the other workbook
 
Upvote 0
When I step though the code it executes every line of my code, then it calls macro from another workbook but I cannot see its code becuase its password protected. After called macro is done a msgbox pops up in password protected workbook saying that macro has been completed and when I click OK yellow highlight disappears and the cursor is just blinking infront of the Application.Run line.

Next part of my code copies results from this macro to my workbook and closes password protected workbook without saving changes but I tried many different lines of code after Application.Run and the result is always the same - macro stops running after calling pass protected code. It seems to me that the fault is hidden in pass protected code - probably End statement after msgbox that pops up on completion of the macro.
 
Upvote 0
If your 2nd workbook closes itself out, then the entire macro truly doesn't run, therefore the 2nd half never gets triggered. Your 2nd workbook macro must reach the END SUB.

IE:

1. Workbook1 -Opens(2nd workbook)
2. Workbook2 - Runs marco

If in workbook2 you have Workbook2.close the END SUB is never triggered.
What you want to do is:

Workbook2.save
End Sub

2nd half should trigger then in 1st workbook

The first line of your 2nd half of code from Workbook1 should be Workbook2.close

Hope this helps someone
 
Upvote 0

Forum statistics

Threads
1,215,101
Messages
6,123,096
Members
449,095
Latest member
gwguy

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