Solved

export access query data to multiple specified worksheet

Posted on 2014-02-04
12
2,644 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
  • 5
  • 4
  • 3
12 Comments
 
LVL 19

Expert Comment

by:Eric Sherman
Comment Utility
<<<<<<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)
Comment Utility
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)
Comment Utility
so you may have to export to 8 workbooks, then combine into a master workbook with 8 sheets.
0
 
LVL 19

Assisted Solution

by:Eric Sherman
Eric Sherman earned 500 total points
Comment Utility
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
Comment Utility
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)
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:alam747
Comment Utility
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
Comment Utility
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
Comment Utility
I used ! not the dollar sign but the message shows same as I mentioned.
0
 
LVL 19

Expert Comment

by:Eric Sherman
Comment Utility
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
Comment Utility
No, while exporting workbook in not open.
0
 

Author Closing Comment

by:alam747
Comment Utility
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
excel file 5 44
Aceess 2016 - Variable Data Fields? 12 39
Excel Macro - Lookup-Highlight 4 24
How to compare data in excel 5 26
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
My experience with Windows 10 over a one year period and suggestions for smooth operation
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now