I found this code works perfectly:
Sub Freeze_Top_Panes()
Application.ScreenUpdating = False
Rows("2:2").Select
ActiveWindow.FreezePanes = True
Application.ScreenUpdating = True
End Sub
To achieve this for all sheets in your workbook, try this:
Sub Freeze_All()
Dim Ws As Worksheet
Application.ScreenUpdating = False
For Each Ws In Application.ActiveWorkbook.Worksheets
Ws.Activate
With Application.ActiveWindow
.FreezePanes = True
End With
Next
Application.ScreenUpdating = True
End Sub
Ii am not sure if this only affects Excel 2007 (as I have not tested it on other versions yet), but it appears that you must turn off screen updating for the freeze panes function to work with VBA. Not sure as to why that is.