使用一个宏在Excel中冻结和解冻第一行

时间:2020-03-13 06:05:16

标签: excel vba

如何仅用一个宏冻结和解冻excel中的第一行? 在这种情况下,请单击一次宏以冻结第一行,然后再次单击以取消冻结?

2 个答案:

答案 0 :(得分:4)

要执行此操作,需要在/其他条件下检查顶部行是否冻结。 下面的代码执行此操作,并假设您要冻结第1行-而不是当前最上面的VISIBLE行。 如果确实要冻结最上面的VISIBLE行,则应删除“ Range(“ A2”)。Select“行。

Sub Freezetop()

' sets cell (current cell) to go back to later
Dim r As Range
Set r = ActiveCell

' if panes not already frozen (splitrow=1 means top row frozen) it freezes them
' going to a2 and back means that what gets frozen is top row of the sheet, NOT the top currently visible row
If Not ActiveWindow.SplitRow = 1 Then
    Range("A2").Select
    With ActiveWindow
    .FreezePanes = False
    .ScrollRow = 1
    .ScrollColumn = 1
    .FreezePanes = True
' uncomment the below if you want you current active row to go to top of page
'    .ScrollRow = r.Row
End With

' unfreezes panes if they were frozen already
Else: ActiveWindow.FreezePanes = False

End If

r.Select

End Sub

答案 1 :(得分:0)

冻结和解冻第一行的两个代码:

Sub Freeze()

    With ThisWorkbook.Worksheets("Sheet1")

        'Active the sheet you want
        .Activate
        'To remove any panes
        ActiveWindow.FreezePanes = False
        'Select the second row
        .Rows("2:2").Select
        'Apple freeze
        ActiveWindow.FreezePanes = True

    End With

End Sub

Sub UnFreeze()

    With ThisWorkbook.Worksheets("Sheet1")

        'Active the sheet you want
        .Activate
        'Remove freeze
        ActiveWindow.FreezePanes = False

    End With

End Sub