Running Macro from another workbook when Application.Run doesn't work

USAMax

Well-known Member
Joined
May 31, 2006
Messages
849
Office Version
  1. 365
Platform
  1. Windows
I have been using the Application.Run for a while but something changed and now it won't work!

The only thing I changed is that I added the same command in another section of the procedure and I tried to use a goto to run this code but it skips the Application.Run without an error. Anybody have any ideas?

Code:
            Workbooks.Open Filename:=Range("InclusionTablePath") & _
                        "\" & Range("InclusionTableFile")   '          Open the AT Inclusion Table workbook
            Set wbAT = ActiveWorkbook                       '          Get the properties for the AT Inclusion Table workbook
            strTest = wbAT.Name
            Application.Run ("'" & strTest & "'!GetNewRequest") 'Update the Attrition Table List
            Application.DisplayAlerts = False               '          Disable Alerts and Warnings
            wbAT.Save                                       '          Save the workbook
            wbAT.Close                                      '          Close the workbook
            Application.DisplayAlerts = True                '          Enable Alerts and Warnings
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
step through the code and verify the string value you are passing into the Run function is valid. Also you can avoid referencing the ActiveWorkbook to make your code easier to follow...

Code:
Set wbAT = Workbooks.Open Filename:=Range("InclusionTablePath") & "\" & Range("InclusionTableFile")
 
Upvote 0
Thank you cerfani,

I like your approach but your command gives me a Compile Error error saying, "Expected: end of statement" and highlighting Filename.

Working off of your idea I tried this and it would not open the workbook.

Code:
            Set wbAT = Range("InclusionTablePath") & "\" & Range("InclusionTableFile")
            Workbooks.Open wbAT
This skips the open line just as the Application.Run did.
 
Upvote 0
that code is wrong...

This is a string value...

Code:
Range("InclusionTablePath") & "\" & Range("InclusionTableFile")

You can't set it to wbAT. You should declare variables to make things easier to follow because you are mixing up types.

Code:
Dim strTest As String 'make sure you use these correctly
Dim wbAT As Workbook

Set wbAT = Workbooks.Open Filename:=Range("InclusionTablePath") & "\" & Range("InclusionTableFile")   '          Open the AT Inclusion Table workbook
strTest = wbAT.Name
Application.Run ("'" & strTest & "'!GetNewRequest") 'Update the Attrition Table List
Application.DisplayAlerts = False               '          Disable Alerts and Warnings
wbAT.Save                                       '          Save the workbook
wbAT.Close                                      '          Close the workbook
Application.DisplayAlerts = True                '          Enable Alerts and Warnings
 
Upvote 0
cerfani,

The issue was that I added a call to a private sub within the sub GetNewRequest. My assumption is that an routine from another workbook should not be allowed to access a private sub in another workbook either directly or indirectly.

Thanks again for the help. Would still like to know how to open the workbook and set the variable at the same time the way you intended.
 
Upvote 0
Workbooks.Open returns a workbook reference so look at the code i posted, that is all you do... You use the Set keyword and save the value to a workbook variable.

Code:
Dim wbk As Workbook
Set wbk = Workbooks.Open "C:\something.xlsx"
 
Upvote 0
@cerfani: ALmost correct, but it needs some brackets:
Code:
Set wbAT = Workbooks.Open(Filename:=Range("InclusionTablePath").Value & "\" & Range("InclusionTableFile").Value)
And I prefer not to rely on the range objects default property and explicitly use .Value so I added that as well.
 
Upvote 0
Oh thanks, I usually rely on the compiler to say something when it comes to using or not using () with function calls
 
Upvote 0
Thank you cerfani and jkpieterse!

cerfani, if it wasn't for you I never would have tried to set the variable while opening the workbook.

jkpieterse, I very much appreciate your completing the final step in cerfani's solution.

Thank you both!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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