Calling a Macro from another workbook

I am running into a problem where I have defined the Workbook as "Destbook" and am trying to call it but getting an error?

The code is here
Rich (BB code):
 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")

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.
 
Last edited:
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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"
 
Upvote 0
Try this instead: Application.Run "'" & Destbook.Name & "'!Macro1"

It worked out perfectly.

Thank you for your help. May I ask what the ".Name" refers too? Is this a way to tell VBA that Destbook is a defined Name?
 
Upvote 0
Glad it helped!

In your code Destbook is workbook object.
But you’ve missed its declaration: Dim Destbook As Workbook
That object has some properties including the Name property.
Destbook.Name means the name of workbook object.

For more details try this code and follow the instruction in the comments:
Rich (BB code):

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
 
Upvote 0
any ideas why this is breaking now that the new code has been applied?

Getting a 'Runtime Error:1004 Application-Defined or Object-Defined Error'


Your help has made my project come to life so I appreciated it greatly. Thanks

Rich (BB code):
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
 
Upvote 0
Getting a 'Runtime Error:1004 Application-Defined or Object-Defined Error'

Rich (BB code):
     If Err.Number <> 0 Then _
At least the underscore symbol after Then is a typo - delete it and try again.
 
Upvote 0
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
 
Last edited:
Upvote 0
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

On my source sheet Range("B14:H14").Select was what the Macro recoreder brought when I used that to get my Copy criteria.

I used to have the code
Range("B14:H & Lastrow").Select but it seemed to not work sometimes due to copying data from a table.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top