VBA to run macro in another workbook

stuartmacdonald

New Member
Joined
May 26, 2009
Messages
48
I'm trying to use VBA to open another workbook, run a macro in it and then close the workbook. Should be simple.

This workbook where I want to run the macro cannot be a macro-enabled workbook because I'm using it with PowerApps and will also be used by other people, so I need to store the macro in a separate workbook.

So far I have the following code:

I open Refresh.xlsm and run this code to open Project check.xlsx and then run the macro

Code:
[/COLOR][COLOR=#333333]Sub openBk()

Workbooks.Open "C:\Users\Stuart MacDonald\OneDrive - Cles\Order book\Project check.xlsx"
Application.Run "C:\Users\Stuart MacDonald\OneDrive - Cles\Order book\Refresh.xlsm!Refresh"


End Sub[/COLOR]
[COLOR=#333333]


Macro
Code:
[/COLOR][COLOR=#333333]Sub Refresh()'
' Refresh Macro
'


'
Windows("Project check.xlsx").Activate
Cells.Select
Range("CLES_project_check[[#Headers],[Contract_No&Desc]]").Activate
Selection.ClearContents
ActiveWorkbook.Connections("Query - CLES_project_check").Refresh
With ActiveSheet.ListObjects.Add(SourceType:=4, Source:=ActiveWorkbook. _
Connections("Query - CLES_project_check"), Destination:=Range("$A$8")). _
TableObject
.RowNumbers = False
.PreserveFormatting = True
.RefreshStyle = 1
.AdjustColumnWidth = True
.ListObject.DisplayName = "CLES_project_check"
.Refresh
End With
ActiveWorkbook.Save
ActiveWindow.Close
End Sub[/COLOR]
[COLOR=#333333]



It opens the workbook fine, but then will not run the macro. Excel returns an error 'Cannot run the macro... the macro may not be available in this workbook or all macros may be disabled'

I have checked the macro security settings in Project check.xlsx and they are set to 'enable all' and trust access to VBA....

Any ideas why it won't run?
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Sub Refresh()'' Refresh Macro
Windows("Project check.xlsx").Activate
Cells.Select
Maybe because you try to run it on .xlsx and file might need to be macro-enabled .xlsm
 
Upvote 0
Dont think that could be the reason as when I have the two workbooks open, I can run the macro from Refresh.xlsm no problem and Project Check.xlsx is updated. I just want the macro to automate it for me...
 
Last edited:
Upvote 0
now youve take the apostrophe out, it should be

application.run "Refresh.xlsm!Refresh"

If the file is open and you provide the full link it won't work.
 
Upvote 0
Since according to your description, both listed macros (openBk & Refresh) are in the same Refresh.xlsm workbook, what happens if you dispense with the application.run statement and instead call Refresh directly?

Code:
Sub openBk()

Workbooks.Open "C:\Users\Stuart MacDonald\OneDrive - Cles\Order book\Project check.xlsx"
Refresh

End Sub
 
Upvote 0
I had same kind of isssue and my dirty workaround had been to have the "refresh" macro launched on an event, a hidden sheet that I would activate, which would call the macro and then hide the sheet again
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,442
Members
448,898
Latest member
drewmorgan128

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