You are on page 1of 3

wks.Range("A1:Q1").

AutoFilter ' EXCEL WORKSHEET METHOD

wks.Rows("1:1").Select ' EXCEL WORKSHEET METHOD

xls.ActiveWindow.FreezePanes = True ' EXCEL APPLICATION METHOD

VBA

Consider the adjusted VBA module complete with error handling

Public Sub ExportExcel()

On Error GoTo ErrHandle

'... incorporate above code ...'

Const outputFileName = pathtoreport & "\" & Me.txtNameTheReport

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _

"GeneralReportWithComments_Pmcs", outputFileName, True

'INITIALIZE EXCEL OBJECTS

Dim xls As Excel.Application

Dim wkb As Excel.Workbook

Dim wks As Excel.Worksheet

Set xls = New Excel.Application

Set wkb = xls.Workbooks.Open(outputFileName)

Set wks = wkb.Worksheets("GeneralReportWithComments_Pmcs")


' FILTER/SORT TOP ROW

wks.Range("A1:Q1").AutoFilter

' FILL FIRST ROW

With wks.Rows("1:1").Interior

.Pattern = xlSolid

.PatternColorIndex = xlAutomatic

.Color = 65535

.TintAndShade = 0

.PatternTintAndShade = 0

End With

'FREEZE TOP ROW

wks.Rows("1:1").Activate

With xls.ActiveWindow

.SplitColumn = 0

.SplitRow = 1

End With

xls.ActiveWindow.FreezePanes = True

'RENAME WORKSHEET

' (WARNING: SPECIAL CHARS LIKE / \ * [ ] : ? NOT ALLOWED IN SHEET NAMES)

wks.Name = Me.txtStartDateTrim & " to " & Me.txtEndDateTrim & "_PMCS"

MsgBox "Successfully exported and formatted workbook!", vbInformation, "OUTPUT"


ExitHandle:

wkb.Close True

Set wks = Nothing: Set wkb = Nothing

xls.Quit

Set xls = Nothing

Exit Sub

ErrHandle:

MsgBox Err.Number & " - " & Err.Description, vbCritical, "RUNTIME ERROR"

Resume ExitHandle

End Sub

You might also like