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 ?
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Octonet

New Member
Joined
Sep 5, 2020
Messages
21
Office Version
  1. 2019
Platform
  1. Windows
Yes mrshl9898, that's right. I saw this too, but this was the error message I got.
All the workbooks in that directory are in fact the same, with only one difference, and that is the weeknumber at the end of the name.
They all have XLSM extension. This is why the macro first looks for the latest saved file.
Strange that it wants to search in my documents directory :rolleyes:
 

mrshl9898

Well-known Member
Joined
Feb 6, 2012
Messages
1,650
A shot in the dark, can't test anything atm.

VBA Code:
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
LatestFile = Replace(LatestFile, "xls", "xlsm")
Application.Run "'" & Mypath & LatestFile & "'!New_week"
End Sub
 

Octonet

New Member
Joined
Sep 5, 2020
Messages
21
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

mrshl9898, this wants to open the latest file; but now I get the strange error "Can not find xxxxxx,xlsmm".
Strange that it says "xlsmm". That double m at the end makes me crazy :ROFLMAO:
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
The red line is both superfluous and causing the error ...

Rich (BB code):
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
LatestFile = Replace(LatestFile, "xls", "xlsm")
Application.Run "'" & Mypath & LatestFile & "'!New_week"
End Sub
 
Solution

Octonet

New Member
Joined
Sep 5, 2020
Messages
21
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

Now I changed the line into :
Application.Run "'Mypath & LatestFile.xlsm'!New_week"
Now the error I get makes more sense "Can not find Mypath & LatestFile.xlsm.
But it is strange that he can not find the file, because the previous line says to open it, and that works without problems?
But I can not get the macro to run a macro in that open latest file. :unsure:
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
Apart from removing the red line, @mrshl9898's code should not be changed.

There's a significant but easy to overlook difference between your
VBA Code:
Application.Run "'Mypath & LatestFile.xlsm'!New_week"

and his
VBA Code:
Application.Run "'" & Mypath & LatestFile & "'!New_week"
 

Octonet

New Member
Joined
Sep 5, 2020
Messages
21
Office Version
  1. 2019
Platform
  1. Windows
Many thanks GWteB, that was the solution :biggrin:
I'm just a beginner and have a lot to learn.
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,295
Office Version
  1. 2013
Platform
  1. Windows
You are welcome and good luck. At some time we all were beginners.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,194
Messages
5,600,257
Members
414,374
Latest member
akbir

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