How can I programmatically freeze the top row of an Excel worksheet in Excel 2007 VBA?

前端 未结 6 904
终归单人心
终归单人心 2020-12-06 04:10

I am looking to programmatically freeze the top row of an Excel worksheet from VBA. The end goal is to produce the same effect as the View > Freeze Panes > Free

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 04:55

    Rows("2:2").Select
    ActiveWindow.FreezePanes = True
    

    This is the easiest way to freeze the top row. The rule for FreezePanes is it will freeze the upper left corner from the cell you selected. For example, if you highlight C10, it will freeze between columns B and C, rows 9 and 10. So when you highlight Row 2, it actually freeze between Rows 1 and 2 which is the top row.

    Also, the .SplitColumn or .SplitRow will split your window once you unfreeze it which is not the way I like.

提交回复
热议问题