+ Reply to Thread
Results 1 to 11 of 11

Copy worksheet from outside workbook using path located in cell

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Copy worksheet from outside workbook using path located in cell

    Hello,

    I have some code here that isn't working and I'm not sure why... I'm trying to copy a worksheet from an outside workbook (the path and filename are located in B2 and B3 of the "Info" tab. I keep getting an "Compile error: Expected: end of statement" message when it gets to the ":=" in the Set CopyFromWbk line.

    Dim fPath As String
    Dim fName As String
    Dim CashFile As String
    fPath = Sheets("Info").Range("B2").Value
    fName = Sheets("Info").Range("B3").Value
    CashFile = fPath & "\" & fName
    Dim CopyFromBook As String
    Dim CopyToWbk As Workbook
    Dim ShToCopy As Worksheet
    Set CopyFromWbk = Workbooks.Filename:=CashFile
    Set ShToCopy = CopyFromWbk.Worksheets("IAG")
    Set CopyToWbk = ActiveWorkbook
     
    ShToCopy.Copy After:=CopyToWbk.Sheets(CopyToWbk.Sheets.Count)
    If someone could help, that'd be great.

    Thanks!

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy worksheet from outside workbook using path located in cell

    Does Range("B3") have a file extension?

  3. #3
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Re: Copy worksheet from outside workbook using path located in cell

    Yep. It's .xlsx and it's located in the cell with the rest of the filename.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy worksheet from outside workbook using path located in cell

    Maybe?

    Sub freybe06()
    Dim fPath As String
    Dim fName As String
    Dim CashFile As String
    fPath = Sheets("Info").Range("B2").Value
    fName = Sheets("Info").Range("B3").Value
    CashFile = fPath & "\" & fName
    Dim CopyFromBook As String
    Dim CopyToWbk As Workbook
    Dim ShToCopy As Worksheet
    Set CopyFromWbk = Workbooks(CashFile)
    Set ShToCopy = CopyFromWbk.Worksheets("IAG")
    Set CopyToWbk = ActiveWorkbook
     
    ShToCopy.Copy After:=CopyToWbk.Sheets(CopyToWbk.Sheets.count)
    End Sub

  5. #5
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Re: Copy worksheet from outside workbook using path located in cell

    I'm getting a "Subscript out of range" error for "Set CopyFromWbk = Workbooks(CashFile)"

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy worksheet from outside workbook using path located in cell

    Are you intending to add a workbook? Or does it already exist and your trying to open it?

  7. #7
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Re: Copy worksheet from outside workbook using path located in cell

    Both workbooks already exist. There is the CopyTo workbook (the one with the macro) and the CopyFrom workbook (the one that the macro opens up to take a spreadsheet from).

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy worksheet from outside workbook using path located in cell

    Then maybe using your first code.

    Dim fPath As String
    Dim fName As String
    Dim CashFile As String
    fPath = Sheets("Info").Range("B2").Value
    fName = Sheets("Info").Range("B3").Value
    CashFile = fPath & "\" & fName
    Dim CopyFromBook As String
    Dim CopyToWbk As Workbook
    Dim ShToCopy As Worksheet
    Set CopyFromWbk = Workbooks.Open Filename:=CashFile
    Set ShToCopy = CopyFromWbk.Worksheets("IAG")
    Set CopyToWbk = ActiveWorkbook
     
    ShToCopy.Copy After:=CopyToWbk.Sheets(CopyToWbk.Sheets.Count)
    You left off the .Open

  9. #9
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Re: Copy worksheet from outside workbook using path located in cell

    Thats giving me the "Compile error: Expected: end of statement" message on that line again...

  10. #10
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    228

    Re: Copy worksheet from outside workbook using path located in cell

    I think I got it... I ran in to a new issue where the worksheet was copying to the new workbook instead of the workbook with the macro, but I got around that by changing the order of the dimensions/sets. Here is what I'm using:

    Dim CopyToWbk As Workbook
    Dim CashFileWbk As Workbook
    Set CopyToWbk = ActiveWorkbook
    Set CashFileWbk = Workbooks.Open(Sheets("Info").Range("B2").Value & "\" & Sheets("Info").Range("B3").Value)
    Dim CopyFromBook As String
    Dim ShToCopy As Worksheet
    Set CopyFromWbk = CashFileWbk
    Set ShToCopy = CopyFromWbk.Worksheets("123")
    
    ShToCopy.Copy After:=CopyToWbk.Sheets(CopyToWbk.Sheets.Count)
    Worksheets("Cash").Delete
    Worksheets("123").Name =  "Cash"
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Resize(, 4).Select
    Selection.Copy
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    
    CashFileWbk.Close

  11. #11
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy worksheet from outside workbook using path located in cell

    Glad to hear you found a solution!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Trying to Open Files Not Located in Specified Path
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-06-2013, 12:40 AM
  2. Replies: 4
    Last Post: 05-03-2012, 06:23 PM
  3. Converting worksheet to PDF with name located in cell B2
    By jonvanwyk in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-14-2011, 12:27 PM
  4. extract workbook/worksheet at server path
    By tango in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-07-2009, 08:57 PM
  5. Open WB located in an unknown path - VBA
    By ElmerS in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 06-22-2009, 04:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1