hi loomah,
I'm taking some data from an access query and then send it to excel and then (trying) to format the cells. The code is here:
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
Dim RecordQuantity As String
DoCmd.SetWarnings False
'setup query to get all records from a table
strSql = "SELECT * FROM [Qry_Room];"
RecordQuantity = DCount("[Room]", "qry_Room") + 3
Set dBase = CurrentDb()
Set rs = dBase.OpenRecordset(strSql, dbOpenDynaset)
Set rs2 = dBase.OpenRecordset(strSQL2, dbOpenDynaset)
'Create Excel object
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Add
xlApp.Visible = True
Set Sht = xlApp.ActiveWorkbook.Sheets(1)
uRow = 4
' Loop through the records and copy them to worksheet.
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
'Here's where the formatting happens
With Sht
.Name = "Give the sheet a name"
'set cell format to general
.Range("A1:A200").NumberFormat = "@"
.Columns("A").Autofit
.Rows("4:" & RecordQuantity).RowHeight = 36
' 'make titles bold
.Range("A1:F1").Font.Bold = True
.Range("G1:I1").Merge
' alignment
' .Rows("2").HorizontalAlignment = xlVAlignCenter
'.Rows("4:" & RecordQuantity).VerticalAlignment = xlVAlignCenter
.Columns("B").NumberFormat = "hh:mm"
.Columns("C").NumberFormat = "hh:mm"
.Columns("F").NumberFormat = "dd:mm:yyyy"
.Columns("B").Autofit
.Columns("C").Autofit
.Cells.Interior.ColorIndex = "2"
.Cells(1, 1).Interior.ColorIndex = "15"
End With
Sht.SaveAs "C:\uffa.xls"
Set Sht = Nothing
xlApp.Quit
Set xlApp = Nothing
Set rs = Nothing
Set dBase = Nothing
End Sub