I have a macro in workbook A that calls a macro in workbook B. I want the macro in workbook B to run and then I want to close workbook B. I keep getting an error saying the macro cannot be found that I want to run from workbook B. I am pretty much a novice at this, but I have done a pretty thorough search and haven't been able to come up with anything on my own. Here is my code in it's entirety.
Public Sub InputDept()
Dim Cap As Workbook
Dim Cap2 As String
On Error Resume Next
Set Cap = Workbooks("NGD Source File for Net Budget Reporting.xlsx")
Cap2 = Cap.Name
On Error GoTo 0
Dim wb As Workbook
Dim Cap1 As Variant
Application.ScreenUpdating = False
If Cap Is Nothing Then
Cap1 = Application.GetOpenFilename("Excel Files(*.xl*)," & "*.xl*", 1)
If Cap1 = False Then
Exit Sub
End If
Set wb = Workbooks.Open(Cap1)
Cap2 = ActiveWorkbook.Name
Else
Workbooks(Cap2).Activate
End If
Sheets("Dept Summary").Activate
Cells.Find(What:="Direct", after:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Dim cRng As Range
Dim dRng As Range
Set dRng = Selection
For Each cRng In dRng
If cRng.Interior.ThemeColor = xlThemeColorAccent3 Then
Dim mCalc As String
Dim mSum As Workbook
On Error Resume Next
Set mSum = Workbooks("Master Calc with Macro.xlsm")
mCalc = mSum.Name
On Error GoTo 0
Application.ScreenUpdating = False
If mSum Is Nothing Then
mSum1 = Application.GetOpenFilename("Excel Files.xl*),"& "*.xl*", 1)
If mSum1 = False Then
Exit Sub
End If
Set wb1 = Workbooks.Open(mSum1)
mCalc = ActiveWorkbook.Name
Else
Workbooks(mCalc).Activate
End If
cRng.Copy
Workbooks(mCalc).Activate
Sheets("Data").Select
Range("A5").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Report").Activate
Workbooks(mCalc).Application.Run ("!SummarizeMaster")
Sheets("Report").Select
ActiveSheet.Copy
Cells.Select
Cells.Copy
Selection.PasteSpecial Paste:=xlPasteValues
ActiveWorkbook.SaveAs _
Filename:=Application.ThisWorkbook.Path & "\" & Format(Date - 28, "MMM") & " Files\" & Left(cRng, 7) & ".xlsx"
ActiveWorkbook.Close
Workbooks(mCalc).Close savechanges:=False
End If
Next cRng
End Sub
This line:
Workbooks(mCalc).Application.Run ("!SummarizeMaster")
needs to be changed a little. You need to include the name of the workbook inside a single quotes, even if it looks like you are specifying the proper workbook with Workbooks(mCalc):
Workbooks(mCalc).Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")
You can actually just shorten it to:
Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")
If the macro you need to find relative macro path by using workbook path from which you run macro and you need to run several macros from the array list, the code below will help:
Dim relativePath As String, programFileName As String
Dim selectedProgramsFiles() As String, programsArrayLastIndex As Byte, I As Byte
For I = 0 To programsArrayLastIndex 'Loop through all selected programs
programFileName = selectedProgramsFiles(I)
relativePath = ThisWorkbook.Path & "\" & programFileName
Workbooks.Open Filename:=relativePath
Application.Run ("'" & relativePath & "'!ModuleName.Main")
Workbooks(programFileName).Activate
ActiveWorkbook.Close SaveChanges:=False
Next I 'For I = 0 To programsArrayLastIndex 'Loop through all selected program
Application.Run "PERSONAL.xlsb!ClearYellow", 0
ClearYellow is the name of the sub in Personal.xlsb that is being run.
The "0" is the first argument of this sub (would omit if no arguments, could add more arguments separated by commas)
Application does not seem to be needed
This could be used to run from some other workbook also; the workbook would have to be open; if the name of that workbook had a space in it, the name would have to be surrounded by ''
Call does not work cross workbooks; haven’t tested within same workbook or within same module
Related
This error has been bugging me out for a while now and while running the macros individually, they always work. Since it was a bit of a work to run 10 macros individually, I decided to compile them into one module and can be run on a click of a form button. After doing that, I've been getting this error on the ActiveSheet.Paste for all of the macros. Any help would be appreciated. Thank you.
Sub ItemCode()
'
' AutoFilter Macro
' Scan itemCode
Dim WB As Workbook
Set WB = ActiveWorkbook
ActiveSheet.Range("C1").AutoFilter Field:=3, Criteria1:=Array("#DIV/0!", "#N/A", "#Ref", "Null"), Operator:=xlFilterValues
If Range("C1").End(xlDown).Value = "" Then
Call CustCode
Else
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Windows("SampleFile.xlsx").Activate
ActiveWorkbook.Sheets.Add.Name = "ItemCode"
Worksheets("ItemCode").Activate
ActiveSheet.Paste
Windows("ABF_Sample.xlsx").Activate
Application.CutCopyMode = False
Selection.AutoFilter
Application.StatusBar = "20% Completed"
End If
End Sub
Note: SampleFile.xlsx was declared in another macro.
Try this code. You will need to change the workbook and sheet names in the code to suit. It's always best to declare your workbooks and sheet names to avoid problems with pasting values in the wrong place. It also means you don't have to activate workbooks and worksheets which really slows down you code.
Sub ItemCode()
'
' AutoFilter Macro
' Scan itemCode
Dim WB1 As Workbook
Set WB1 = Workbooks("ABF_Sample")
Dim WS1 As Worksheet
Set WS1 = WB.Sheets("Sheet1")
Dim FilePath As String
Dim Filename As String
FilePath = "enter your file path here"
Filename = "SampleFile"
Dim WB2 As Workbook
Set WB2 = Workbooks.Add
ActiveWorkbook.SaveAs (FilePath & Filename & ".xlsx")
WS1.Range("C1").AutoFilter Field:=3, Criteria1:=Array("#DIV/0!", "#N/A", "#Ref", "Null"), Operator:=xlFilterValues
If WS.Range("C1").End(xlDown).Value = "" Then
Call CustCode
Else
WS1.UsedRange.Copy
With Workbooks(Filename)
.Sheets.Add.Name = "ItemCode"
.Sheets("ItemCode").Range("A1").PasteSpecial Paste:=xlPasteValues
End With
Application.CutCopyMode = False
Selection.AutoFilter
Application.StatusBar = "20% Completed"
End If
End Sub
I have a macro in workbook A that calls a macro in workbook B. I want the macro in workbook B to run and then I want to close workbook B. I keep getting an error saying the macro cannot be found that I want to run from workbook B. I am pretty much a novice at this, but I have done a pretty thorough search and haven't been able to come up with anything on my own. Here is my code in it's entirety.
Public Sub InputDept()
Dim Cap As Workbook
Dim Cap2 As String
On Error Resume Next
Set Cap = Workbooks("NGD Source File for Net Budget Reporting.xlsx")
Cap2 = Cap.Name
On Error GoTo 0
Dim wb As Workbook
Dim Cap1 As Variant
Application.ScreenUpdating = False
If Cap Is Nothing Then
Cap1 = Application.GetOpenFilename("Excel Files(*.xl*)," & "*.xl*", 1)
If Cap1 = False Then
Exit Sub
End If
Set wb = Workbooks.Open(Cap1)
Cap2 = ActiveWorkbook.Name
Else
Workbooks(Cap2).Activate
End If
Sheets("Dept Summary").Activate
Cells.Find(What:="Direct", after:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Dim cRng As Range
Dim dRng As Range
Set dRng = Selection
For Each cRng In dRng
If cRng.Interior.ThemeColor = xlThemeColorAccent3 Then
Dim mCalc As String
Dim mSum As Workbook
On Error Resume Next
Set mSum = Workbooks("Master Calc with Macro.xlsm")
mCalc = mSum.Name
On Error GoTo 0
Application.ScreenUpdating = False
If mSum Is Nothing Then
mSum1 = Application.GetOpenFilename("Excel Files.xl*),"& "*.xl*", 1)
If mSum1 = False Then
Exit Sub
End If
Set wb1 = Workbooks.Open(mSum1)
mCalc = ActiveWorkbook.Name
Else
Workbooks(mCalc).Activate
End If
cRng.Copy
Workbooks(mCalc).Activate
Sheets("Data").Select
Range("A5").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Report").Activate
Workbooks(mCalc).Application.Run ("!SummarizeMaster")
Sheets("Report").Select
ActiveSheet.Copy
Cells.Select
Cells.Copy
Selection.PasteSpecial Paste:=xlPasteValues
ActiveWorkbook.SaveAs _
Filename:=Application.ThisWorkbook.Path & "\" & Format(Date - 28, "MMM") & " Files\" & Left(cRng, 7) & ".xlsx"
ActiveWorkbook.Close
Workbooks(mCalc).Close savechanges:=False
End If
Next cRng
End Sub
This line:
Workbooks(mCalc).Application.Run ("!SummarizeMaster")
needs to be changed a little. You need to include the name of the workbook inside a single quotes, even if it looks like you are specifying the proper workbook with Workbooks(mCalc):
Workbooks(mCalc).Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")
You can actually just shorten it to:
Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")
If the macro you need to find relative macro path by using workbook path from which you run macro and you need to run several macros from the array list, the code below will help:
Dim relativePath As String, programFileName As String
Dim selectedProgramsFiles() As String, programsArrayLastIndex As Byte, I As Byte
For I = 0 To programsArrayLastIndex 'Loop through all selected programs
programFileName = selectedProgramsFiles(I)
relativePath = ThisWorkbook.Path & "\" & programFileName
Workbooks.Open Filename:=relativePath
Application.Run ("'" & relativePath & "'!ModuleName.Main")
Workbooks(programFileName).Activate
ActiveWorkbook.Close SaveChanges:=False
Next I 'For I = 0 To programsArrayLastIndex 'Loop through all selected program
Application.Run "PERSONAL.xlsb!ClearYellow", 0
ClearYellow is the name of the sub in Personal.xlsb that is being run.
The "0" is the first argument of this sub (would omit if no arguments, could add more arguments separated by commas)
Application does not seem to be needed
This could be used to run from some other workbook also; the workbook would have to be open; if the name of that workbook had a space in it, the name would have to be surrounded by ''
Call does not work cross workbooks; haven’t tested within same workbook or within same module
I have a list of *.xlsm file names on a sheet named "DB" in range E961 to E1010 (50 rows) and I'm trying to create a macro that runs through this list and open the corresponding files in the set directory, runs some code and close the file, moving on to the next file on the list - repeating this operation every 5 minutes.
The directory contains 400+ xlsm files, and the list in E961 will typically be less than 50 files - so I'm not trying to open all the files in the directory. That already happens once a day at a set time.
But I am trying to open these "shortlisted" files and update them every 5 minutes for example. I tried different combinations of code but can't seem to get it working.
The main file containing this code is also in the same directory to allow relative linking to the other 400+ files, hence the ThisWorkbook.Path code.
Edited code below:
Sub UPDATE()
Application.ScreenUpdating = True
With ThisWorkbook.Worksheets("DB")
Dim inputRange As Range
Set inputRange = .Range("E961", .Cells(.Rows.Count, 5).End(xlUp))
End With
Dim directory As String: directory = ThisWorkbook.Path & "\"
Dim fileName As String
Dim r As Range
Dim xlwb As Workbook
For Each r In inputRange
If r <> vbNullString Then
fileName = Dir(directory & r & ".xl??*")
Set xlwb = Workbooks.Open(directory & fileName)
Application.DisplayAlerts = False
ActiveWorkbook.RefreshAll
If Range("A4") > Sheets("DB").Cells(Rows.Count, "A").End(xlUp).Offset(0) Then
Worksheets("DB").Range("A4:L4").Select
Worksheets("DB").Range("A4").Activate
Selection.Copy
Sheets("DB").Select
Sheets("DB").Cells(Rows.Count, "A").End(xlUp).Offset(1). _
PasteSpecial Paste:=xlPasteValues, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
Else
End If
If Range("A4") = Sheets("DB").Cells(Rows.Count, "A").End(xlUp).Offset(0) Then
Worksheets("DB").Range("A4:L4").Select
Worksheets("DB").Range("A4").Activate
Selection.Copy
Sheets("DB").Select
Sheets("DB").Cells(Rows.Count, "A").End(xlUp).Offset(0). _
PasteSpecial Paste:=xlPasteValues, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
End If
xlwb.Close True
End If
Next r
Application.ScreenUpdating = True
End Sub
The error comes from "Set xlwb = (sht.Cells(Row, 1).Value)" because it is trying to open a sheet as a workbook, but I have no idea how to fix it... or everything is wrong ...
Thanks for the help!
Try this piece it should work thought it will only open and close workbooks until you give it some code to work them:
Option Explicit
Sub UPDATE()
Application.ScreenUpdating = False
'if you are only using here your wb and sht variables, use a With, there is no need to use variables
With ThisWorkbook.Worksheets("DB")
Dim inputRange As Range
'It is preferable to do xlUp because you could find some empty cells in between.
Set inputRange = .Range("E961", .Cells(.Rows.Count, 5).End(xlUp))
End With
Dim directory As String: directory = ThisWorkbook.Path & "\"
Dim fileName As String
Dim r As Range
Dim xlwb As Workbook
For Each r In inputRange
If r <> vbNullString Then
fileName = Dir(directory & r & ".xl??*") 'don't know if your cell has the extension
Set xlwb = Workbooks.Open(directory & fileName)
'some code
xlwb.Close False 'False won't save the workbook, use True if you want it to be saved.
End If
Next r
Application.ScreenUpdating = True
End Sub
I have a program that daily exports between 8-12 Excel workbooks with different names.
In each of these workbooks is a sheet called sheet "A".
These workbooks are placed in a folder on the user's desktop and the VBA will prompt the user to locate said folder. It will then file by file open each workbook and add a sheet called "Enter Construction Data" and then another sheet called "schedule".
When these sheets are opened one at a time it will copy formulas (A1:AZ1000) from a master Excel file (different workbook (which also has sheets named "Enter Construction Data" and "Schedule") and copy them into the workbooks the user placed in the folder on their desktop (the ones that contain sheet "A").
In summary I am adding sheets from a master Excel workbook to other Excel workbooks, then copying formulas into them and breaking the sheet references.
All of the above works, as intended.
However, when I run the code the file picker opens for every workbook twice to update sheet "A". I do not want to edit or update sheet "A" and if I click cancel in the file folder dialogue box, it wont.
My goal is to eliminate the need for the user to select the "cancel" button in the file folder picker dialogue box. (2x's each file times 8 - 12 times a day)
Bonus: Some of my files have arrays and the compatibility checker pops up. Is there is a way to default to convert using VBA?
I believe the error lies in either the set sh line or the set nws line.
Private Sub BtnAddWorksheets_Click()
Dim file_count As Long
Dim file_name As String
Dim check_path As String
Dim count_files As Integer
Dim NewWB As Workbook
Dim MasterWB As Workbook
Dim sh As Worksheet
Dim i As Integer
Dim x As Integer
check_path = Navigator.TxtFilePath
MsgBox (check_path)
file_name = Dir(check_path & "\" & "*")
file_count = 0
'MsgBox (check_path & "\" & file_name) - verified this is the full path
Do While file_name <> ""
'open workbook
Set MasterWB = ThisWorkbook
Set NewWB = Workbooks.Open(Filename:=check_path & "\" & file_name)
'The correct Excel file opens
'Ensure workbook has opened before doing next line of codes
'
NewWB.Sheets.Add After:=NewWB.Worksheets(NewWB.Worksheets.Count)
ActiveSheet.Name = ("Enter Construction Data")
Set sh = ThisWorkbook.Worksheets("Enter Construction Data")
Set nws = Sheets("Enter Construction Data")
With sh.Cells.Copy
End With
With nws.Cells
.PasteSpecial Paste:=xlPasteFormulas
.PasteSpecial Paste:=xlPasteAllUsingSourceTheme
'Application.CutCopyMode = False
End With
'Eliminate the Previous workbook reference
Sheets("Enter Construction Data").range("A1:AZ1000").Select
Selection.Replace What:="[NavigatorFormBuild.xlsm]", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set sh = Nothing
Set nws = Nothing
NewWB.Sheets.Add After:=NewWB.Worksheets(NewWB.Worksheets.Count)
ActiveSheet.Name = ("Schedule")
Set sh = ThisWorkbook.Worksheets(3)
Set nws = Sheets("Schedule")
With sh.Cells.Copy
End With
With nws.Cells
.PasteSpecial Paste:=xlPasteFormulas
.PasteSpecial Paste:=xlPasteAllUsingSourceTheme
'Application.CutCopyMode = False
End With
'Eliminate the Previous workbook reference
Sheets("Schedule").range("A1:AZ1000").Select
Selection.Replace What:="[NavigatorFormBuild.xlsm]", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
NewWB.Close (savechanges = False)
Set MasterWB = Nothing
Set NewWB = Nothing
file_count = file_count + 1
file_name = Dir
If count_files = file_count Then
Exit Sub
End If
Loop
count_files = -1
End Sub
This dialogue opens two times per worksheet - This is the only thing I am trying to eliminate from the code.
Image showing dialogue from code run.
Image showing window with error missing source for updating tab "A".
The file picker opens because of the links to the master workbook. Remove the replace code and add this before the workbook is saved.
' remove links
msg = ""
aLinks = NewWB.LinkSources
If Not IsEmpty(aLinks) Then
For j = 1 To UBound(aLinks)
If InStr(aLinks(j), "[NavigatorFormBuild.xlsm]") > 0 Then
NewWB.BreakLink aLinks(j), xlLinkTypeExcelLinks
msg = msg & vbCr & aLinks(j)
End If
Next j
If Len(msg) > 0 Then MsgBox "Links broken " & msg, vbInformation
End If
I found the answer! This website helped me get to the bottom of the problem.
https://learn.microsoft.com/en-us/office/troubleshoot/excel/control-startup-message
This was a multi-faceted problem. First I didn't realize that the sheet index for sheet "A" in the one workbook was (1) and in the other workbook it was (9). To fix this, I simply added a sheet to the workbook missing a sheet index(1) and allowed the default name "Sheet1" to stay. Once I did this, I had a sheet with index (1) in both workbooks. This enabled the "break link" button in the following window:
Edit Links Window - Referenced in attached article.
Next, I clicked the "break link" button and searched the sheets for #REF! as #CDP1802 referenced above. This lead me to a single cell "A1" in the "Enter Construction Data" worksheet. I retyped the formula: =A!A1 in this cell and the next time I executed the VBA it only opened up the windows compatibility checker (at this point the sheet order was messed up, to fix I simply deleted "Sheet1" - the sheet that was created to get the shared index.) Now the array formulas carry over between workbooks perfectly.
#CDP1802 - your code may have worked to break the links if these indexes were established in both workbooks when I originally tried to run your code. I very much appreciate your help in solving the issues on this!
Final Code:
Private Sub BtnAddWorksheets_Click()
Dim file_count As Long
Dim file_name As String
Dim check_path As String
Dim count_files As Integer
Dim NewWB As Workbook
Dim MasterWB As Workbook
Dim sh As Worksheet
Dim i As Integer
Dim x As Integer
Dim ExcelFileName As String
ExcelFileName = ThisWorkbook.Name
check_path = Navigator.TxtFilePath
file_name = Dir(check_path & "\" & "*")
file_count = 0
'MsgBox (check_path & "\" & file_name) - verified this is the full path
Application.ScreenUpdating = False
Do While file_name <> ""
'open workbook
Set MasterWB = ThisWorkbook
Set NewWB = Workbooks.Open(FileName:=check_path & "\" & file_name)
'The correct Excel file opens
'Adds sheet "Enter Construction Data" to workbooks
NewWB.Sheets.Add After:=NewWB.Worksheets(NewWB.Worksheets.Count)
ActiveSheet.Name = ("Enter Construction Data")
Set sh = ThisWorkbook.Worksheets(2)
Set nws = Sheets("Enter Construction Data")
With sh.Cells.Copy
End With
With nws.Cells
.PasteSpecial Paste:=xlPasteFormulas
.PasteSpecial Paste:=xlPasteAllUsingSourceTheme
End With
'''''Eliminate the Previous workbook reference
Sheets("Enter Construction Data").Range("A1:AZ1000").Select
Selection.Replace What:="[" & ExcelFileName & "]", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
sbProtectSheet
Set sh = Nothing
Set nws = Nothing
'Adds sheet "Schedule" to workbooks
NewWB.Sheets.Add After:=NewWB.Worksheets(NewWB.Worksheets.Count)
ActiveSheet.Name = ("Schedule")
Set sh = ThisWorkbook.Worksheets(3)
Set nws = Sheets("Schedule")
With sh.Cells.Copy
End With
With nws.Cells
.PasteSpecial Paste:=xlPasteFormulas
.PasteSpecial Paste:=xlPasteAllUsingSourceTheme
End With
''''''Eliminate the Previous workbook reference
Sheets("Schedule").Range("A1:CA100").Select
Selection.Replace What:="[" & ExcelFileName & "]", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
sbProtectSheet
Worksheets("A").Activate
sbProtectSheet
Worksheets("Enter Construction Data").Activate
ActiveWorkbook.CheckCompatibility = False
NewWB.Close SaveChanges:=True
Set MasterWB = Nothing
Set NewWB = Nothing
file_count = file_count + 1
file_name = Dir
If count_files = file_count Then
count_files = -1
Exit Sub
End If
Loop
Application.ScreenUpdating = True
Unload Navigator
End Sub
Thank you SO Community!
I have a macro in workbook A that calls a macro in workbook B. I want the macro in workbook B to run and then I want to close workbook B. I keep getting an error saying the macro cannot be found that I want to run from workbook B. I am pretty much a novice at this, but I have done a pretty thorough search and haven't been able to come up with anything on my own. Here is my code in it's entirety.
Public Sub InputDept()
Dim Cap As Workbook
Dim Cap2 As String
On Error Resume Next
Set Cap = Workbooks("NGD Source File for Net Budget Reporting.xlsx")
Cap2 = Cap.Name
On Error GoTo 0
Dim wb As Workbook
Dim Cap1 As Variant
Application.ScreenUpdating = False
If Cap Is Nothing Then
Cap1 = Application.GetOpenFilename("Excel Files(*.xl*)," & "*.xl*", 1)
If Cap1 = False Then
Exit Sub
End If
Set wb = Workbooks.Open(Cap1)
Cap2 = ActiveWorkbook.Name
Else
Workbooks(Cap2).Activate
End If
Sheets("Dept Summary").Activate
Cells.Find(What:="Direct", after:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Dim cRng As Range
Dim dRng As Range
Set dRng = Selection
For Each cRng In dRng
If cRng.Interior.ThemeColor = xlThemeColorAccent3 Then
Dim mCalc As String
Dim mSum As Workbook
On Error Resume Next
Set mSum = Workbooks("Master Calc with Macro.xlsm")
mCalc = mSum.Name
On Error GoTo 0
Application.ScreenUpdating = False
If mSum Is Nothing Then
mSum1 = Application.GetOpenFilename("Excel Files.xl*),"& "*.xl*", 1)
If mSum1 = False Then
Exit Sub
End If
Set wb1 = Workbooks.Open(mSum1)
mCalc = ActiveWorkbook.Name
Else
Workbooks(mCalc).Activate
End If
cRng.Copy
Workbooks(mCalc).Activate
Sheets("Data").Select
Range("A5").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Report").Activate
Workbooks(mCalc).Application.Run ("!SummarizeMaster")
Sheets("Report").Select
ActiveSheet.Copy
Cells.Select
Cells.Copy
Selection.PasteSpecial Paste:=xlPasteValues
ActiveWorkbook.SaveAs _
Filename:=Application.ThisWorkbook.Path & "\" & Format(Date - 28, "MMM") & " Files\" & Left(cRng, 7) & ".xlsx"
ActiveWorkbook.Close
Workbooks(mCalc).Close savechanges:=False
End If
Next cRng
End Sub
This line:
Workbooks(mCalc).Application.Run ("!SummarizeMaster")
needs to be changed a little. You need to include the name of the workbook inside a single quotes, even if it looks like you are specifying the proper workbook with Workbooks(mCalc):
Workbooks(mCalc).Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")
You can actually just shorten it to:
Application.Run ("'Master Calc with Macro.xlsm'!SummarizeMaster")
If the macro you need to find relative macro path by using workbook path from which you run macro and you need to run several macros from the array list, the code below will help:
Dim relativePath As String, programFileName As String
Dim selectedProgramsFiles() As String, programsArrayLastIndex As Byte, I As Byte
For I = 0 To programsArrayLastIndex 'Loop through all selected programs
programFileName = selectedProgramsFiles(I)
relativePath = ThisWorkbook.Path & "\" & programFileName
Workbooks.Open Filename:=relativePath
Application.Run ("'" & relativePath & "'!ModuleName.Main")
Workbooks(programFileName).Activate
ActiveWorkbook.Close SaveChanges:=False
Next I 'For I = 0 To programsArrayLastIndex 'Loop through all selected program
Application.Run "PERSONAL.xlsb!ClearYellow", 0
ClearYellow is the name of the sub in Personal.xlsb that is being run.
The "0" is the first argument of this sub (would omit if no arguments, could add more arguments separated by commas)
Application does not seem to be needed
This could be used to run from some other workbook also; the workbook would have to be open; if the name of that workbook had a space in it, the name would have to be surrounded by ''
Call does not work cross workbooks; haven’t tested within same workbook or within same module