Option Explicit
Sub RunMacro_NoArgs()
Dim PathToFile As String, _
NameOfFile As String, _
wbTarget As Workbook, _
CloseIt As Boolean
NameOfFile = "MyMacroLivesHere.xls"
PathToFile = "C:\temp"
On Error Resume Next
Set wbTarget = Workbooks(NameOfFile)
If Err.Number <> 0 Then
Err.Clear
Set wbTarget = Workbooks.Open(PathToFile & "\" & NameOfFile)
CloseIt = True
End If
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
Application.Run (wbTarget.Name & "!MacroName")
If CloseIt = True Then
wbTarget.Close savechanges:=False
Else
ThisWorkbook.Activate
End If
End Sub
Sub RunMacro_WithArgs()
Dim PathToFile As String, _
NameOfFile As String, _
wbTarget As Workbook, _
MyResult As Variant, _
CloseIt As Boolean
NameOfFile = "MyFunctionLivesHere.xls"
PathToFile = "C:\temp"
On Error Resume Next
Set wbTarget = Workbooks(NameOfFile)
If Err.Number <> 0 Then
Err.Clear
Set wbTarget = Workbooks.Open(PathToFile & "\" & NameOfFile)
CloseIt = True
End If
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
MyResult = Application.Run(wbTarget.Name & "!Functionname", 1, 2)
MsgBox MyResult
If CloseIt = True Then
wbTarget.Close savechanges:=False
Else
ThisWorkbook.Activate
End If
End Sub
|