Cell reference file path

Fredek

Board Regular
Joined
Mar 8, 2011
Messages
65
Hi guys,

Short question today, in cell A2 I have the file I want the VBA to open:

e.g. C:\Users\user1\Desktop\Book1.xlsx

How do I go about using information in cell A2 to open the file? I know it has to be Workbooks.Open Filename... but what comes next?

Code:
Workbooks.Open Filename:=


Thank you!


<tbody>
</tbody><colgroup><col></colgroup>

<tbody>
</tbody><colgroup><col></colgroup>

<tbody>
</tbody><colgroup><col></colgroup>
 
Hi Fluff, added the variable "Set StrtSht = ActiveSheet", now I get the runtime error... Apologies Fluff, I'm a total beginner..


Code:
Sub Tmp()
    
    Dim SrcWbk As Workbook
    Dim StrtSht As Worksheet
    
    
    Set StrtSht = ActiveSheet
     Worksheets("GMCR Extract").ListObjects("GMCR_tbl").AutoFilter.ShowAllData
     
     Set SrcWbk = Workbooks.Open(Filename:=StrtSht.Range("U3").Value)
   
    
    With SrcWbk.Sheets("Tbl_M_GMCR_Daily_Positions")
        .Range("A1", .Range("R" & Rows.Count).End(xlUp)).Copy _
             Workbooks("Book1.xlsm").Worksheets("GMCR Extract").Range("A2")
    End With
End Sub
 
Last edited:
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Is the file you're opening an .xls file?
 
Upvote 0
When I try this:

Code:
Sub Tmp()
    
    Dim SrcWbk As Workbook
    Dim StrtSht As Worksheet
    Dim DestWbk As Workbook
    
    
    Set StrtSht = ActiveSheet
     Worksheets("GMCR Extract").ListObjects("GMCR_tbl").AutoFilter.ShowAllData
     
     Set SrcWbk = Workbooks.Open(Filename:=StrtSht.Range("U3").Value)
   
   
    With SrcWbk.Sheets("Tbl_M_GMCR_Daily_Positions")
        .Range("A1", .Range("R" & Rows.Count).End(xlUp)).Copy _
            DestWbk.Sheets("GMCR Extract").Range("A2")
    End With
End Sub

The destination worksheet in U17 is already open - it is the original worksheet that holds the macro. I would like the destination to be another sheet of the open workbook.
 
Last edited:
Upvote 0
Object definer error:


Code:
Sub Tmp()
    
    Dim SrcWbk As Workbook
    Dim StrtSht As Worksheet
    Dim DestWbk As Workbook
    
    
    Set StrtSht = ActiveSheet
    Set DestWbk = ActiveWorkbook
     Worksheets("GMCR Extract").ListObjects("GMCR_tbl").AutoFilter.ShowAllData
     
     Set SrcWbk = Workbooks.Open(Filename:=StrtSht.Range("U3").Value)
   
   
    With SrcWbk.Sheets("Tbl_M_GMCR_Daily_Positions")
        .Range("A1", .Range("R" & Rows.Count).End(xlUp)).Copy _
            DestWbk.Sheets("GMCR Extract").Range("A2")
    End With
End Sub
 
Last edited:
Upvote 0
Are the files either xlsx or xlsm files?
 
Upvote 0
In that case you need to put a . in front of the rows.count
Code:
.Range("A1", .Range("R" & .Rows.Count).End(xlUp)).Copy
otherwise rows.count will return 1048576 which is beyond the range of an xls workbook
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,930
Members
449,479
Latest member
nana abanyin

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