Run macro from another workbook

Octonet

New Member
Joined
Sep 5, 2020
Messages
27
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 ?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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:
 
Upvote 0
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
 
Upvote 0
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:
 
Upvote 0
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
 
Upvote 0
Solution
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:
 
Upvote 0
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"
 
Upvote 0
Many thanks GWteB, that was the solution :biggrin:
I'm just a beginner and have a lot to learn.
 
Upvote 0
You are welcome and good luck. At some time we all were beginners.
 
Upvote 0

Forum statistics

Threads
1,213,479
Messages
6,113,897
Members
448,530
Latest member
yatong2008

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