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

前端 未结 6 900
终归单人心
终归单人心 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:34

    The problem with the recorded macro is the same as the problem with the built-in action: Excel chooses to freeze the top visible row, rather than the actual top row where the header information can be found.

    The purpose of a macro in this case is to freeze the actual top row. When I am viewing row #405592 and I need to check the header for the column (because I forgot to freeze rows when I opened the file), I have to scroll to the top, freeze the top row, then find my way back to row #405592 again. Since I believe this is stupid behavior, I want a macro to correct it, but, like I said, the recorded macro just mimics the same stupid behavior.

    I am using Office 2011 for Mac OS X Lion

    Update (2 minutes later):

    I found a solution here: http://www.ozgrid.com/forum/showthread.php?t=19692

    Dim r As Range 
    Set r = ActiveCell 
    Range("A2").Select 
    With ActiveWindow 
        .FreezePanes = False 
        .ScrollRow = 1 
        .ScrollColumn = 1 
        .FreezePanes = True 
        .ScrollRow = r.Row 
    End With 
    r.Select 
    

提交回复
热议问题