Hey everyone,
I'm hoping someone can help me out troubleshooting some code I found online (from Ken Puls).
Context:
- I have 2 workbooks ('Master' and 'Dash'). The first ('Master') compiles and summarizes data and the second ('Dash') imports this summarized data and presents it in a more 'user friendly' format.
- I have a series of macros that run in 'Master' and I have a macro in 'Dash' that imports from 'Master' to 'Dash'
Purpose of desired macro:
- I would like to integrate the macro that imports from 'Master' to 'Dash' (currently residing in the 'Dash' workbook) into the a macro in the 'Master' workbook.
Solution so far (which results in an error):
-The following code which I tried to run from 'Master' is intended to open 'Dash' and run its 'Exprt' macro. Unfortunately it results in an error ("Run-Time error '1004': Cannot run the macro [...] The macro may not be available in this workbook or all macros may be disabled") at this line:
Application.Run (wbTarget.Name & "!Exprt")
The complete macro code is below:
Option Explicit
Sub RunMacro_NoArgs()
'Macro purpose: Use the application.run method to execute
'a macro without arguments from another workbook
Dim PathToFile As String, _
NameOfFile As String, _
wbTarget As Workbook, _
CloseIt As Boolean
'Set file name and location. You will need to update this info!
NameOfFile = "Dash.xlsm"
PathToFile = "F:\TEST"
'Attempt to set the target workbook to a variable. If an error is
'generated, then the workbook is not open, so open it
On Error Resume Next
Set wbTarget = Workbooks(NameOfFile)
If Err.Number <> 0 Then
'Open the workbook
Err.Clear
Set wbTarget = Workbooks.Open(PathToFile & "\" & NameOfFile)
CloseIt = True
End If
'Check and make sure workbook was opened
If Err.Number = 1004 Then
MsgBox "Sorry, but the file you specified does not exist!" _
& vbNewLine & PathToFile & "\" & NameOfFile
Exit Sub
End If
On Error GoTo 0
'Run the macro! (You will need to update "MacroName" to the
'name of the macro you wish to run)
Application.Run (wbTarget.Name & "!Exprt")
If CloseIt = True Then
'If the target workbook was opened by the macro, close it
wbTarget.Close savechanges:=False
Else
'If the target workbook was already open, reactivate this workbook
ThisWorkbook.Activate
End If
End Sub
Any ideas to fix this issue? I've tried a few variation of that line causing the error and nothing worked so far.
Thank you,
Goeff.
Bookmarks