I can't figure why my workbook won't freeze panes at A2 so that my column headers are always showing in a scroll. Everything else works.
Here's the whole code:
Here's the whole code:
Code:
Private Sub cmdExport_Click()
On Error Resume Next
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlWorkbook As Excel.Workbook
Dim acQuery As QueryDef
Dim objRST As Recordset
Dim strQryName As String
Dim strSheetName As String
' Set the form's mouse pointer to hourglass to show data is compiling.
Screen.MousePointer = 11
strQueryName = Me.List1
strSheetName = Left(strQueryName, 31)
strSheetName = Trim(strSheetName)
Set xlApp = New Excel.Application
xlApp.Visible = False
Set xlWorkbook = xlApp.Workbooks.Add
Set objRST = Application.CurrentDb.OpenRecordset(strQueryName)
'Add column headers to excel output
Set xlSheet = xlWorkbook.Sheets(1)
For lvlColumn = 0 To objRST.Fields.count - 1
xlSheet.Cells(1, lvlColumn + 1).Value = objRST.Fields(lvlColumn).Name
'Change the font to bold for the header row
xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(1, objRST.Fields.count)).Font.Bold = True
'FREEZE HEADER CELLS - THIS ISN"T WORKING
With xlSheet.Range("A2").Select
ActiveWindow.FreezePanes = True
End With
'Add Border to header row cells
With xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(1, objRST.Fields.count)).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(1, objRST.Fields.count)).Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(1, objRST.Fields.count)).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(1, objRST.Fields.count)).Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'Autofit columns
Set xlSheet = xlBook.Sheets(strQueryName)
xlSheet.Cells.EntireColumn.AutoFit
'Name worksheet
With xlSheet
.Range("A2").CopyFromRecordset objRST
.Name = strSheetName
End With
Next
xlApp.Visible = True
Set objRST = Nothing
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlApp = Nothing
' Set the form's mouse pointer back to the default mouse pointer.
Screen.MousePointer = 0
End Sub