jason_kelly
02-06-2011, 09:11 PM
Hi There,
I need your help.
I have the code below that works to export data in an ADO Recordset to an Excel file, however, I would like it to be modifiied such that the Column names/headers are also included when I export the data from access to the excel file.
Is it also possible to modiy the code as well, such that when the data is exported to excel that all the rows in Excel are the same height?
'-------------------------------------------------------
Public Sub ExportTOExcel()
'-------------------------------------------------------
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
Dim j As Long
Dim lngCol As Long
Dim lngRow As Long
Dim Field As Object
rs.MoveFirst
lngRow = 1
With oSheet
Do While Not rs.EOF
lngCol = 0
For Each Field In rs.Fields
lngCol = lngCol + 1
.Cells(lngRow, lngCol) = Field.Value
Next
lngRow = lngRow + 1
rs.MoveNext
Loop
End With
oBook.SaveAs "C:\Book5.xls"
Workbooks.Open fileName:="C:\Book5.xls"
End Sub
Any help with this is greatly appreciated.
Much thanks in advance,
Cheers,
Jay
I need your help.
I have the code below that works to export data in an ADO Recordset to an Excel file, however, I would like it to be modifiied such that the Column names/headers are also included when I export the data from access to the excel file.
Is it also possible to modiy the code as well, such that when the data is exported to excel that all the rows in Excel are the same height?
'-------------------------------------------------------
Public Sub ExportTOExcel()
'-------------------------------------------------------
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
Dim j As Long
Dim lngCol As Long
Dim lngRow As Long
Dim Field As Object
rs.MoveFirst
lngRow = 1
With oSheet
Do While Not rs.EOF
lngCol = 0
For Each Field In rs.Fields
lngCol = lngCol + 1
.Cells(lngRow, lngCol) = Field.Value
Next
lngRow = lngRow + 1
rs.MoveNext
Loop
End With
oBook.SaveAs "C:\Book5.xls"
Workbooks.Open fileName:="C:\Book5.xls"
End Sub
Any help with this is greatly appreciated.
Much thanks in advance,
Cheers,
Jay