Hi,
Try the following, it is an extract form one of my modules and I do this frequently. If you need more as to how to format Excel let me know.
Dim objXL As Object
Dim objWkb As Object
Dim objSht As Object
Dim DB As Database
Dim rs As Recordset
Dim intLastCol As Integer
Dim intLastRow As Integer
Dim strWhat As String, boolXl As Boolean
Dim mychart As Chart
Dim ActiveChart As Chart
Dim strRecords As String
intLastCol = 0
intLastRow = 0
Const conMAX_ROWS = 20000
Const conSHT_NAME = "Boiler 1 Data"
Const conWKB_NAME = “C:\DATA\Boiler pH Annual Chart.xls"
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
'Check if Excel is running
If fIsAppRunning("Excel") Then
Set objXL = GetObject(, "Excel.Application")
boolXl = False
Else
Set objXL = CreateObject("Excel.Application")
boolXl = True
End If
Set DB = CurrentDb
Set rs = DB.OpenRecordset("qryBoilerpH1", dbOpenSnapshot)
'Calculate size of speadsheet
intLastCol = rs.Fields.Count '+ rs1.Fields.Count
If rs.RecordCount > 0 Then
rs.MoveLast: rs.MoveFirst
intLastRow = rs.RecordCount + 6
Set objWkb = GetObject(conWKB_NAME)
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
'Transfer Boiler 1 information to Excel into worksheet "Boiler 1 Data"
With objXL
.Visible = True 'False
Set objWkb = .Workbooks.Open(conWKB_NAME)
Set objSht = objWkb.Worksheets("Boiler 1 Data")
objWkb.Worksheets("Boiler 1 Data").Activate
If Not Err.Number = 0 Then
Set objSht = objWkb.Worksheets.Add
objSht.name = "Boiler 1 Data"
End If
Err.Clear
On Error GoTo 0
With objSht
'Clear worksheet from previous data The additional values is to make sure there is nosupprises
.Range(.Cells(1, 1), .Cells(intLastRow + 800, _
intLastCol + 30)).ClearContents
.Range(.Cells(1, 1), .Cells(intLastRow + 100, _
intLastCol)).Font.Bold = False
'Copy data from the record set (query)
.Range("A2").CopyFromRecordset rs
'Add column headings
.Cells(1, 1).Value = "Sample Date "
.Cells(1, 2).Value = "pH"
.Cells(1, 3).Value = "pH 1 High"