+ Reply to Thread
Results 1 to 2 of 2

Export from active worksheet to closed workbook

Hybrid View

mbcluck Export from active worksheet... 07-22-2009, 10:38 AM
romperstomper Re: Export from active... 07-22-2009, 10:54 AM
  1. #1
    Registered User
    Join Date
    07-22-2009
    Location
    usa
    MS-Off Ver
    Excel 2003
    Posts
    1

    Export from active worksheet to closed workbook

    Is there a way to export a range of data of my current worksheet to the next blank row on a closed workbook? I have a template excel file used to place orders and I want to copy the order information (ex B3:F3) to the next available row in a workbook that has all of the orders.

    Thanks

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,012

    Re: Export from active worksheet to closed workbook

    Under certain circumstances, yes. You can use ADO to export data from the activeworkbook to a sheet in a closed workbook as long as that destination workbook has headers, and as long as you include a header row in the source that you export from. For example:
    Sub ExportDataFromThisWorkbookToClosedWorkbook()
       ' Sample demonstrating how to export data from the current workbook to a closed workbook
       Dim cn As Object, strQuery As String
       Set cn = CreateObject("ADODB.Connection")
       With cn
          .Provider = "Microsoft.Jet.OLEDB.4.0"
          .ConnectionString = "Data Source=" & ActiveWorkbook.FullName & ";" & _
             "Extended Properties=""Excel 8.0;HDR=Yes;"""
          .Open
       End With
       strQuery = "INSERT INTO [Sheet1$] IN '' [Excel 8.0;Database=C:\ADO Dest.xls] SELECT * FROM [Sheet1$B2:F3]"
    
       cn.Execute strQuery
       cn.Close
       Set cn = Nothing
    End Sub
    Note that there is a memory leak issue with using ADO on an open workbook, but as long as you only run one or two exports at a time (and then close Excel) it shouldn't be a problem.
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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