I have 4 queries that I am exporting to Excel from Access.
My goals are to:
choose the file path (check)
Export the queries into separate sheets (sort of)
Format the data as a table (help!)
I want to do all this from a single button click in Access using VBA
I have successfully exported the queries using
Expand|Select|Wrap|Line Numbers
- DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Query1", filename, True
I am not sure if this is a problem for later or not.
I've tried recording a MACRO in excel and editing that to get my formatting done, but I've heard that I should try to avoid "Select" and don't know enough about the library to get around that.
I've scoured the web and found a start, but this only formats cells A1 and A2. I have added the Excel Object Library.
Expand|Select|Wrap|Line Numbers
- Public Function formatTable(filename As String)
- 'MsgBox "In format", vbExclamation, ""
- Dim objXLApp As Excel.Application
- Dim objXLBook As Excel.Workbook
- Dim objXLSheet As Excel.Worksheet
- 'open the Excel spreadsheet with the exported data
- Set objXLApp = CreateObject("Excel.Application")
- 'for testing, make the application visible
- objXLApp.Visible = True
- Set objXLBook = objXLApp.Workbooks.Open(filename)
- MsgBox "here 1 "
- objXLBook.Sheets("Query1").ListObjects.Add(xlSrcRange, Range("$A$1"), , xlYes).Name = "Table1"
- MsgBox "here 2"
- objXLBook.Sheets("Query1").ListObjects("Table1").TableStyle = "TableStyleMedium9"
- objXLApp.PrintCommunication = False
- 'save the changes
- objXLBook.Save
- objXLApp.Quit
- Set objXLApp = Nothing
- End Function
The data will always have the same number of columns but the number of rows will change.
Please help me learn how to use VBA smartly and format my output.
I appreciate the help.