Results 1 to 7 of 7

Thread: [RESOLVED] Insert a spreadsheet from other workbook

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    12

    Resolved [RESOLVED] Insert a spreadsheet from other workbook

    How can I insert a spreadsheet from other workbook using VBA? Depending on parameters I would set a code to insert a certain spreadsheet from an external workbook. I just couldn't find the "insert" procedure.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Insert a spreadsheet from other workbook

    Excel VBA question moved to Office Development

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Insert a spreadsheet from other workbook

    Would the source and target workbooks be open already or would you want the procedure to open them?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Insert a spreadsheet from other workbook

    I did a quick Macro Record and extracted the following:
    Code:
        Workbooks("Book1Name").Sheets("SheetName").Copy Before:=Workbooks("Book2Name").Sheets(1)
        Workbooks("Book1Name").Sheets("SheetName").Move Before:=Workbooks("Book2Name").Sheets(1)
    This requires that you specify all names explicitly ... you can substiture string variables with the names if you need to. This makes the Moved/Copied sheet the first sheet in the destination workbook. To make the Moved/Copied sheet the LAST sheet, just make the following substitution:
    Code:
    Sheets(1)  >>  Sheets(Workbooks("Book2Name").Sheets.Count)
    You can make things easier by assigning handles to your Workbooks and Worksheets. Let me know if you need to know how to do that.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Insert a spreadsheet from other workbook

    IF both books are closed use the following...

    VB Code:
    1. Sub CopySheet(sSourcePath As String, sTargetPath As String, sSheetName As String)
    2. Dim wkbSource As Workbook
    3. Dim wkbTarget As Workbook
    4. Dim lSheetCount As Long
    5.  
    6.     Workbooks.Open Filename:=sSourcePath
    7.     Set wkbSource = ActiveWorkbook
    8.    
    9.     Workbooks.Open Filename:=sTargetPath
    10.     Set wkbTarget = ActiveWorkbook
    11.    
    12.     lSheetCount = wkbTarget.Sheets.Count
    13.    
    14.     wkbSource.Worksheets(sSheetName).Copy After:=wkbTarget.Sheets(lSheetCount)
    15.    
    16.     wkbSource.Close SaveChanges:=False
    17.     wkbTarget.Close SaveChanges:=True
    18.  
    19.     Set wkbSource = Nothing
    20.     Set wkbTarget = Nothing
    21. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    12

    Re: Insert a spreadsheet from other workbook

    Thanks Kenny, I'try it. Target book will be open. The source will be closed.
    Vladimir

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    12

    Re: Insert a spreadsheet from other workbook

    Hi Kenny, it worked. I have changed it a little bit.

Posting Permissions

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



Click Here to Expand Forum to Full Width