Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exporting a query/table to excel and formatting it automatically 1

Status
Not open for further replies.

Davide77

Technical User
Mar 6, 2003
166
CH
Do you have an idea on how to automatically export and format a table from access to excel. Formatting: change colors of specific cells, merge cells and this kind of things.
thanks
 
Whilst this can be programmed from access (have a look at the transferspreadsheet method), it can also be done FROM excel even more easily, using Data>Get External data. Choose Access as your driver and follow the wizard...

Rgds, Geoff
[blue]Experience is something you don't get until just after you need it[/blue]
We want to help [red]you[/red] Help us by reading this FAQ 1st faq222-2244
 
Hallo xlbo,
I looked the transferspreadsheet method but it doesn't seem to allow formatting of the output table...
 
Nope - you'd have to do the transfer in code and then open up the excel object to format it - that's why I suggested having a look at pulling the data in FROM excel. If that is not an option, I can provide some sample code for working with the excel object

Rgds, Geoff
[blue]Experience is something you don't get until just after you need it[/blue]
We want to help [red]you[/red] Help us by reading this FAQ 1st faq222-2244
 
xlbo, would be nice if you post some code that allow to handle excel tables from access..

thanks
 
ok - this is from a similar thread - it exports the contents of a table to excel:
Code:
Sub Export_To_Excel()

Dim strSql As String
Dim dBase As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer
Dim uRow As Integer
Dim xlApp As Object
Dim Sht As Object
 
    DoCmd.SetWarnings False
    [COLOR=green] 'setup query to get all records from a table[/color]
    strSql = "SELECT * FROM [TWPW AGM];"

    Set dBase = CurrentDb()
    Set rs = dBase.OpenRecordset(strSql, dbOpenDynaset)
    [COLOR=green] 'Create Excel object[/color]
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Workbooks.Add
    xlApp.Visible = True
    Set Sht = xlApp.ActiveWorkbook.Sheets(1)
        uRow = 1
       With rs
        [COLOR=green] ' Loop through the field names[/color]
        For i = 0 To .Fields.Count - 1
            Sht.Cells(uRow, i + 1).Value = rs.Fields(i).Name
        Next i
        uRow = 2
       End With
        [COLOR=green] ' Loop through the records and copy them to worksheet.[/color]
        Do Until rs.EOF

            For i = 0 To rs.Fields.Count - 1
                Sht.Cells(uRow, i + 1).Value = rs(i)
            Next i
            rs.MoveNext
            uRow = uRow + 1

        Loop
        [COLOR=green] 'Here's where the formatting happens[/color]
        With Sht
           .Name = "Give the sheet a name"
              [COLOR=green] 'set cell format to general[/color]
           .Range("A1:A200").NumberFormat = "@"
              '[COLOR=green] 'make titles bold[/color]
           .Range("A1:F1").Font.Bold = True
        End With
[COLOR=green] 'Other stuff[/color]
ActiveWorkbook.SaveAs "Full Path & Name go here"
[COLOR=green] 'Clear variables[/color]
Set Sht = Nothing
xlApp.Quit
Set xlApp = Nothing
Set rs = Nothing
Set dBase = Nothing
End Sub

Rgds, Geoff
[blue]Experience is something you don't get until just after you need it[/blue]
We want to help [red]you[/red] Help us by reading this FAQ 1st faq222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top