Solved

export access query data to multiple specified worksheet

Posted on 2014-02-04
12
2,729 Views
Last Modified: 2014-04-23
Hi Experts,

I am trying to export data from access query to specific sheets of an excel workbook.

Below is the VBA code I am using:

    Kill "c:\test\test_report.xlsx"   delete the old report
    FileCopy "c:\test\work\test_report_work.xlsx", "c:\test\test_report.xlsx" ' Copy the work file
    xlfile = "c:\test\test_report.xlsx"  
   
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_01", xlfile, True, "test01"
   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_02", xlfile, True, "test02"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_03", xlfile, True, "test03"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_04", xlfile, True, "test04"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_05", xlfile, True, "test05"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_06", xlfile, True, "test06"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_07", xlfile, True, "test07"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_08", xlfile, True, "test08"
 
test_report_work.xlsx workbook contain total of blank 8 sheet named as above(test01 ~test08)
while I execute it only copy data from the query to the same sheet first 4 sheets then add new sheets instead of copy the on the same sheets.


Please advise why its not copy data on the sheet same as first four sheet instead of adding new sheet on the test_report.xlsx workbook.

Your prompt response would be highly appreciated.

Thanks
0
Comment
Question by:alam747
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 39834586
<<<<<<test_report_work.xlsx workbook contain total of blank 8 sheet named as above(test01 ~test08)  while I execute it only copy data from the query to the same sheet first 4 sheets then add new sheets instead of copy the on the same sheets.
Please advise why its not copy data on the sheet same as first four sheet instead of adding new sheet on the test_report.xlsx workbook.>>>

Probably because sheets 1 thru 4 did not exist.  I think Access creates new worksheets inside your existing workbook if the worksheet already exist.  Try deleting all 8 worksheets first then run your export to see if all 8 worksheets will be created.

ET
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 39834621
from MSDN, your use of Transfer sheet is not correct
expression.TransferSpreadsheet(TransferType, SpreadsheetType, TableName, FileName, HasFieldNames, Range, UseOA)

Range  Optional Variant. A string expression that's a valid range of cells or the name of a range in the spreadsheet. This argument applies only to importing. Leave this argument blank to import the entire spreadsheet. When you export to a spreadsheet, you must leave this argument blank. If you enter a range, the export will fail.

so it appears you cant export to a specific sheet within a workbook.
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 39834624
so you may have to export to 8 workbooks, then combine into a master workbook with 8 sheets.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 19

Assisted Solution

by:Eric Sherman
Eric Sherman earned 500 total points
ID: 39834633
Try this ... Including the Sheet Name! with the Range A:A should export the results starting with column A of the existing worksheet.

 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_01", xlfile, True, "test01!A:AZ"

 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "test_report_02", xlfile, True, "test02!A:AZ"


Etc.

Etc.




ET
0
 
LVL 19

Accepted Solution

by:
Eric Sherman earned 500 total points
ID: 39834644
Rob ...

See my post above.  If you include the SheetName! with a valid Column Range A:AZ like ("test01!A:AZ") then the export will write the data to an existing worksheet in the workbook.

ET
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 39834664
mmm.  never tried and cant now as dont have access anymore..

but further searching suggests that etsherman is correct (and MSDN is not...)
http://www.utteraccess.com/forum/lofiversion/index.php/t1989431.html; maybe its a relatively recent ie 2007/2010 update.
0
 

Author Comment

by:alam747
ID: 39835610
Hi etsherman,

I tried the way advised and getting the follwing run time error:

"Table "Test01$A:AE" already exists"

would you please let me know if I missed anything.

Thanks
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 39836918
Use the exclamation point (!) instead of the dollar sign ($) to define the worksheet range as shown below:

"Test01!A:AE"



ET
0
 

Author Comment

by:alam747
ID: 39838571
I used ! not the dollar sign but the message shows same as I mentioned.
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 39839975
This works each time for me ... I am using Office 2010 but should be the same for other versions of Office.  I am exporting the Employee table to Employee.xlsx which includes the worksheet Emp_List.

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "EMPLOYEE", "C:\Temp\Employee.xlsx", True, "Emp_List!A:AZ"


By any chance do you have the workbook open at the time you are running the export???


ET
0
 

Author Comment

by:alam747
ID: 39881455
No, while exporting workbook in not open.
0
 

Author Closing Comment

by:alam747
ID: 40018877
Thanks
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question