Results 1 to 2 of 2
  1. #1
    derekben is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jul 2013
    Posts
    11

    Formatting Access Export to Excel

    Hi,

    I am looking to export a table to excel from access. I would like to order the transaction category column in a specific order(round trip air far, parking, lodging etc),. I have a button that runs a make query table and exports it to excel. I would like the rows to be in the order of transactions category. What code would I need in the button to make this order correct? If you need any extra information, PLEASE ASK!! ANY HELP WOULD BE AMAZING! THANK YOU.

    I have attached some code below. Let me know if you need any extra explanations. THANKS!

    -Derek


    Private Sub ExportDebitsButton_Click()
    Dim oApp As Excel.Application
    Dim oWB As Excel.Workbook
    Dim i As Integer
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim sSQL As String

    DoCmd.OpenQuery "Export Debits to Excel Query", acViewNormal, acEdit

    With DoCmd
    .SetWarnings False
    .OpenQuery "Export Debits to Excel Query"
    .SetWarnings True
    End With



    'Create an instance of Excel and add a new blank workbook
    sSQL = "SELECT * FROM [Export Debits to Excel Table]"
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)


    Set oApp = New Excel.Application
    oApp.Visible = False
    Set oWB = oApp.Workbooks.Add


    'Add the field names as column headers (optional)
    For i = 0 To rst.Fields.Count - 1
    oWB.Sheets(1).Cells(1, i + 1).Value = rst.Fields(i).Name
    Next


    oWB.Sheets(1).Range("1:1").Font.Bold = True
    oWB.Sheets(1).Cells(2, 1).CopyFromRecordset rst
    oWB.Sheets(1).Columns.AutoFit


    oWB.Sheets(1).Range("H1").Value = "RefNumber"


    Dim x As Integer
    x = oWB.Sheets(1).UsedRange.Rows.Count - 1




    For i = 1 To x
    oWB.Sheets(1).Range("H" & i + 1) = oWB.Sheets(1).Range("H2") + i
    Next

    'Clean up ADO Objects
    rst.Close
    Set rst = Nothing


    'Create a folder if not exist
    Dim strFilePath As String
    Dim strFolder As String
    strFolder = "C:\My Documents"
    strFilePath = strFolder & "\AMEX_Debits_" & Format(Now(), "mm-dd-yyyy") & ".xlsx"


    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FolderExists(strFolder) Then
    'Create the file
    FileSystem.MkDir (strFolder)
    End If


    'Clean up Excel Objects
    oWB.Close SaveChanges:=True, FileName:=strFilePath
    Set oWB = Nothing
    oApp.Quit
    Set oApp = Nothing


    'Open the file after export to excel
    Shell "EXCEL.EXE """ & strFilePath & "", vbNormalFocus




    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Why do you make table for the export?

    Export a query that has an ORDER BY clause.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Formatting Access Export to Excel - VBA
    By derekben in forum Import/Export Data
    Replies: 2
    Last Post: 07-01-2013, 02:19 PM
  2. Export Data to Excel Pivot Table Loss of Formatting
    By Damian in forum Import/Export Data
    Replies: 1
    Last Post: 05-17-2013, 07:56 PM
  3. Replies: 7
    Last Post: 04-25-2013, 03:47 PM
  4. Marco: Export to Excel w/o Formatting
    By AKoval in forum Import/Export Data
    Replies: 1
    Last Post: 03-20-2013, 09:14 AM
  5. Excel export formatting
    By tariq nawaz in forum Import/Export Data
    Replies: 0
    Last Post: 09-24-2012, 05:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums