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
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 = 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
Else
End If
Destbook.Activate
Sheets("ODM").Select
Application.Run ("'" & Destbook & "'!macro1")
Try this instead: Application.Run "'" & Destbook.Name & "'!Macro1"Rich (BB code):Application.Run ("'" & Destbook & "'!macro1")
Any ideas why this isn't working? My macro name is Macro1 and Destbook returns the full file name so I am stuck.
My Error. Error: 438 Does not support this property or method.
Try this instead: Application.Run "'" & Destbook.Name & "'!Macro1"
Sub Test()
Dim Destbook As Workbook
Set Destbook = ActiveWorkbook
' In new code line type: Destbook
' then type the dot symbol: Destbook.
' After typing the dot symbol the list of workbook's properties and methods will appear.
' Choose the Name property: Destbook.Name
' and press F1 to see the help of that (selected) property
MsgBox "Destbook.Name = " & Destbook.Name
End Sub
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 = 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
Else
End If
Destbook.Activate
Sheets("ODM").Select
On Error Resume Next
Application.Run "'" & Destbook.Name & "'!Macro1"
If Err.Number <> 0 Then _
Err.Clear
Else
Sheets("ODM").Select
Range("H11").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End If
ThisWorkbook.Activate
Sheets("ODM").Select
Range("B14:H14").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Destbook.Activate
Sheets("ODM").Select
Range("C14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ThisWorkbook.Activate
Sheets("ODM").Select
Range("N14:O14").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Destbook.Activate
Range("J14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
After On Error Resume Next you can't get the error message.
To understand the logic, please explain what range are you trying to select by these two lines of the code:
Range("B14:H14").Select
Range(Selection, Selection.End(xlDown)).Select