Public Sub FormatExcelBasic(fileIn As String, sheetIn As String)
'you must add a reference to excel in your project for this to work
'i.e. Microsoft Excel 11.0 Object Library
'fileIn is the fullpath and name of the excel file
'sheetIn is the name of the worksheet you are trying to format - typically this
'is the first 31 characters of the query you exported with the
'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,... command in a different sub
'if you run this code against any excel spreadsheet: FormatExcelBasic "c:\test.xls", "testsheet"
'the test sheets will end up with autofitted columns and frozen, bolded, aqua headers
On Error GoTo errHan
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
Dim lngLastRow As Long
Dim strCell As String
Set xlApp = New Excel.Application
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open(fileIn)
Set xlSheet = xlBook.Sheets(sheetIn)
'rename the sheet to something more friendly to humans
xlBook.Sheets(sheetIn).NAME = "New Sheet Name"
'this will give you the last row used on the spreadsheet
'in case you want to programatically create a total line - there must
'be something in column A for this to work, of use another column that
'has data in every row
lngLastRow = xlSheet.Range("A65536").End(xlUp).Row
'select the first row
Set xlRange = xlSheet.Rows(1)
'bold the selection, set the font and fontsize, center all the cells
xlRange.Font.Bold = True
xlRange.Font.Size = 10
xlRange.Font.NAME = "Verdana"
xlRange.HorizontalAlignment = xlCenter
'stretch all the cells to 30 - this maks the auto work better
xlSheet.Cells.EntireColumn.ColumnWidth = 30
'autofit the columns
xlSheet.Cells.EntireColumn.AutoFit
'freeze the pane so the header row doesn't scroll
xlSheet.Activate
xlSheet.Range("A2", "A2").Select
xlApp.ActiveWindow.FreezePanes = True
'the following code is some examples of different things to do in VBA
'with the Excel object - some stuff will repeat - this is just a bunch of samples
'do some settins for the page layout when printing
With xlSheet.PageSetup
.LeftHeader = "Left Header Here"
.CenterHeader = "Center Header Here"
.CenterFooter = "Page &P" 'print the page number bottom center
.LeftMargin = xlApp.InchesToPoints(0.25)
.RightMargin = xlApp.InchesToPoints(0.25)
.TopMargin = xlApp.InchesToPoints(1)
.BottomMargin = xlApp.InchesToPoints(1)
.HeaderMargin = xlApp.InchesToPoints(0.5)
.FooterMargin = xlApp.InchesToPoints(0.5)
.PrintQuality = 600
.Orientation = xlLandscape
.PaperSize = xlPaperLegal
.FirstPageNumber = xlAutomatic
.Order = xlOverThenDown
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
.PrintGridlines = True
End With
'some examples of setting formats for columns
Set xlRange = xlSheet.Columns("N:N")
xlRange.NumberFormat = "$#,##0"
Set xlRange = xlSheet.Columns("L:M")
xlRange.NumberFormat = "#,##0"
Set xlRange = xlSheet.Columns("CG:CK")
xlRange.NumberFormat = "0.0%"
'some more formating - sets the entire sheetfont, then bolds the header
Set xlRange = xlSheet.Cells.EntireRow
xlRange.Font.NAME = "Arial"
xlRange.Font.Size = 8
Set xlRange = xlSheet.Rows("1:1")
xlRange.Font.FontStyle = "Bold"
xlRange.Interior.ColorIndex = 8
xlRange.HorizontalAlignment = xlCenter
'This is how you can draw some borders around a selected range
With xlRange.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlRange.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlRange.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlRange.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlRange.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'this is how you can set the headers on specific columns
xlSheet.Cells(1, 1).Value = "Header 1"
xlSheet.Cells(1, 2).Value = "Header 2"
'some more auofitting and freezing
xlSheet.Cells.EntireColumn.ColumnWidth = 30
'autofit the columnms
xlSheet.Cells.EntireColumn.AutoFit
'lock the first row
xlSheet.Activate
xlSheet.Range("A2", "A2").Select
xlApp.ActiveWindow.FreezePanes = True
'need to save all the changes and release all the variables
'*****
'IF YOU DO NOT RELEASE ALL THE VARIABLE IN THE CORRECT ORDER, YOU WILL
'END UP WITH INVISBLE EXCEL SESSIONS THAT NEVER CLOSE
'the invisible sessions can be killed from task manager, but you
'should try to always get the excel object killed before exiting the sub
xlBook.Save
Set xlRange = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Sub
errHan:
MsgBox Err.Number & " - " & Err.Description & vbCrLf & vbCrLf & _
"Error occurred during FormatExcelBasic function.", vbCritical, "Error!"
'if there was an error, need to save whatever changes we made and clear the
'variables
'IF YOU DO NOT RELEASE ALL THE VARIABLE IN THE CORRECT ORDER, YOU WILL
'END UP WITH INVISBLE EXCEL SESSIONS THAT NEVER CLOSE
'the invisible sessions can be killed from task manager, but you
'should try to always get the excel object killed before exiting the sub
On Error Resume Next
xlBook.Save
Set xlRange = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Sub
Resume
End Sub