I have a VBA script in Excel that freezes the panes of an Excel worksheet, but I'm curious to see if this is possible without first selecting a range. Here's by code now which freezes rows 1 through 7:
ActiveSheet.Range("A8").Select
ActiveWindow.FreezePanes = True
Any suggestions?
I found the previous answers only worked with some sheets when
looping
throughtabs
. I found the following code worked on everytab
Ilooped
through (target was a singleworkbook
), despite whichworkbook
was theactiveworkbook
.The short of it:
The code as it is in my
Sub
: (be aware, I do a lot more formatting in this sub, I tried to strip that out and leave just the code needed here)Hopefully, this will save someone some research time in the future.
Record yourself using the View ► Freeze Panes ► Freeze Top Row command and this is what you get for .FreezePanes.
So modifying the .SplitColumn and/or .SplitRow properties should do it for you regardless on what the ActiveCell property is.
I need to be able to properly refreeze panes (when creating new windows, notably) without losing the activecell or messing up the visible range. It took a lot of playing around but I think I have something solid that works:
I know this is old but I came across this tidbit that may be useful... as ChrisB stated, the SplitColumn/SplitRow values represent the last cell above/left of the split BUT of the currently visible window. So if you happen to have code like this:
The split will be between rows 110 and 111 instead of 10 and 11.
edited for clarification and to add more information:
My point is that the values are offsets of the upper left cell, not an address of a cell. Therefore, ChrisB's Dec 4 '15 at 18:34 comment under the main answer only holds if row 1 is visible in the Activewindow.
A couple of other points on this:
CETAB may be dealing with this in their answer.
There are many things to get wrong about freezing panes. I add my own answer, so I will find it here, and won't have to reinvent it next time.
Example usage:
Application.Windows
(Windows(Thisworkbook.Name)
) won't cause an error if you have more windows to the same workbook (the name would be "MyWorkbook:1"), or Excel attempted (which usually fails) to repair a workbook after a crash (the name would be "MyWorkbook [Repaired]")