|
Question : Export Access Data to Excel Template Using VBA
|
|
I use the TransferSpreadsheet method to export Access data to an Excel Template file (.xlt) worksheet called 'DataSheet': Cmd.TransferSpreadsheet acExport, , "qry1ForTandI_FormExportToExcel", strFile, True, "DataSheet" The Excel Template already has worksheet called 'DataSheet' because several other worksheets (one summary and four charts) in the Excel file link to 'DataSheet' to get their data. Note: The user saves the file as .xls.
My problem is that the TransferSpreadsheet code above adds ANOTHER worksheet to the template file, named DataSheet(1), instead of overwriting the current worksheet called 'DataSheet'. Thus the Excel formulas in the other worksheets are referenced to the wrong worksheet ('DataSheet' instead of 'DataSheet(1)'. If I do not include the worksheet 'DataSheet' in the Excel Template file, the references in the Excel formula are broke (because the worksheet 'DataSheet' is absent) and I get the #ref error.
Seem to me I have two options: Overwrite the current 'DataSheet' worksheet to maintain the Excel references or write VBA code in Access to send the Excel cell formulas to the appropriate cells in the other Excel worksheets.
Any ideas? Thanks
|
|
Answer : Export Access Data to Excel Template Using VBA
|
|
try this code to delete the DataSheet
Dim strPath As String strPath = "C:\ExcelFiles\Book1.xls" Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Open("" & strPath & "") Set xlSheet = xlBook.Worksheets("DataSheet") xlApp.Visible = True xlSheet.Delete xlApp.DisplayAlerts = False xlBook.SaveAs FileName:=strPath Set xlSheet = Nothing Set xlBook = Nothing xlApp.Quit Set xlApp = Nothing
|
|
|
|