Run macro from another workbook

Octonet

New Member
Joined
Sep 5, 2020
Messages
21
Office Version
  1. 2019
Platform
  1. Windows
I have workbook A open. In A I have a macro that has to call a macro (New_week) in workbook B.
Problem is that I have only variables as name for B.
Here is the script of the macro in book A.
Sub Downtime_ASL()
'
' Downtime_ASL Macro
'
Dim Mypath As String
Dim Myfile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim GL As Date
Mypath = "I:\Excel\VBA project downtime\ASL"
If Right(Mypath, 1) <> "\" Then Mypath = Mypath & "\"
Myfile = Dir(Mypath & "Downtime*.xlsm", vbNormal)
If Len(Myfile) = 0 Then
MsgBox "Nothing found...", vbExclamation
Exit Sub
End If
Do While Len(Myfile) > 0
GL = FileDateTime(Mypath & Myfile)
If GL > LatestDate Then
LatestFile = Myfile
LatestDate = GL
End If
Myfile = Dir
Loop
Workbooks.Open Mypath & LatestFile
Application.Run "'Mypath & LatestFile'!New_week"
End Sub

In the directory where B is located there are a lot of files. This macro looks for the last saved file and opens it.
In workbook B there is a macro "New_week" that I want to call.
The workbook will be opened, so the variables are known.
But I get errors when I get to the line Application.Run "'Mypath & LatestFile'!New_week".
The error says "In c:\users\documents\ there is nof file "Mypath & LatestFile.xls"

What did I do wrong ?
 

mrshl9898

Well-known Member
Joined
Feb 6, 2012
Messages
1,619
Strange it wouldn't find .xls but must have been an .xlsm extension. Glad you're sorted
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Watch MrExcel Video

Forum statistics

Threads
1,122,312
Messages
5,595,430
Members
413,990
Latest member
Kher83

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
Top