Ricky Morris
Active Member
- Joined
- Mar 31, 2002
- Messages
- 363
What's the syntax to call a macro in another workbook?
Thanks,
Ricardo
Thanks,
Ricardo
It should be Range("B14:H" & Lastrow).Select
Sub ODM_Transfer()
Dim Destbook As Workbook
Dim varCellvalue As Variant
Dim sNameFound As String
Const sPath As String = "\\Lax-Netapp01\Dept_private\Business Systems\LFC MACRO TEST\"
varCellvalue = ThisWorkbook.Sheets("Main").Range("B24").Value
On Error Resume Next
Set Destbook = Workbooks(varCellvalue & ".xls")
If Err.Number <> 0 Then _
Set Destbook = Workbooks(varCellvalue & ".xlsm")
On Error GoTo 0
If Destbook Is Nothing Then
sNameFound = Dir(sPath & varCellvalue & ".xls*")
If sNameFound = vbNullString Then
MsgBox "No files found matching: " & sPath & varCellvalue & ".xls*"
Exit Sub
Else
Set Destbook = Workbooks.Open(sPath & sNameFound)
End If
End If
Destbook.Activate
Sheets("ODM").Select
On Error Resume Next
Application.Run "'" & Destbook.Name & "'!Macro1"
If Err.Number = 0 Then
Destbook.Sheets("ODM").Range("H11").Hyperlinks(1).Follow NewWindow:=False
End If
On Error GoTo 0
With ThisWorkbook.Sheets("ODM")
.Range("B14:H14", .Cells(.Rows.Count, "B").End(xlUp)).Copy
End With
Destbook.Sheets("ODM").Range("C14").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
With ThisWorkbook.Sheets("ODM")
.Range("N14:O14", .Cells(.Rows.Count, "N").End(xlUp)).Copy
End With
Destbook.Sheets("ODM").Range("J14").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Destbook.Sheets("ODM").Range("C14").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
"""If the name of the workbook is dynamically assigned as a variable you can use this code
Application.Run ("'" & workbookname & "'!macroname")
Found the solution
if you try to run a macro from a workbook that contains spaces you must enclose them with apostrophe, such as
Code:Application.Run ("'Analytics macro S17.xlsm'!getdata")
I have tried this over and over again and everytime it tells me
"Can not run the macro 'Weekly Shrinkage Tool.xlsm!NewWeek'. The macro may not be available is this workbook or all macros may be disabled."
I have tried all sorts of versions of the line
Application.Run "MacroBook!MacroName"
including using a variable for the file name which I can't get to work at all
but here are some ways I have typed it
Application.Run "Weekly Shrinkage Tool.xlsm!NewWeek"
Application.Run "Weekly Shrinkage Tool.xlsm!NewWeek()"
Application.Run "Weekly Shrinkage Tool.xlsm!Public Sub NewWeek()"
Application.Run "Weekly Shrinkage Tool!NewWeek"
Am I missing something like a simple sintax error?
If the name of the workbook is dynamically assigned as a variable you can use this code
Application.Run ("'" & workbookname & "'!macroname")