Saturday, November 9, 2019

vba - Freeze multiple columns in excel using macro


Under the Freeze panes, Excel currently offers Freeze panes, Freeze first row and Freeze first column options.
I was looking for an option where I can freeze the first N-columns.
I did a record macro to find out what Excel was doing when Freeze first column option was selected and it showed this:



Sub Macro1()
' Macro1 Macro
With ActiveWindow
.SplitColumn = 1
.SplitRow = 0
End With
ActiveWindow.FreezePanes = True
End Sub

But what actually is happening is a split column than the freeze pane function.


So when I changed the .SplitColumn=4, I ended having a split than freeze panes.


Is there a workaround for this or is this suppose to work only this way?


Answer



This is an interesting question, and I can see your question is actually 2 parts.



  1. Why does the macro choose Split instead of Pane and

  2. How to work around it.


I don't know the answer to 1, so I'll focus on 2 only.


Since you want to unfreeze you need to use freeze!


Sub DoThis()
Columns("E:E").Select
ActiveWindow.FreezePanes = True
End Sub

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...