动态冻结excel中的窗格

mas*_*nix 6 excel

我有一个excel表,其中包含以下格式的数据:

Title
Summary A
A info 1
A info 2
A info 3
Summary B
B info 1
B info 2
Summary C
Run Code Online (Sandbox Code Playgroud)

所以现在我只有"标题"作为excel中的冻结窗格.它工作正常,除了有很多数据,所以当你在工作表的中间很难知道你是在工作summary A还是summary B.

有谁知道如何动态冻结窗格?即,最初titleSummary A将被冻结.然后,当用户向下滚动,直到下一个部分,然后TitleSummary B将被冻结等等.

Tim*_*ams 3

您可以尝试一下(在工作表代码模块中)。每次选择更改时,它都会在第一列中向上检查“摘要*”等内容:如果工作表尚未冻结到该行,它将进行调整。

一个困难是要向上滚动,您必须单击顶部窗格中的某一行......

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Static lastFreeze As Long
    Dim c As Range, r As Long, sel As Range

    Set sel = Selection
    'get the first cell on the selected row, then
    '  go up until find a content like "Summary*"
    Set c = Target.Parent.Cells(Target.Cells(1).Row, 1)
    Do While (Not c.Value Like "Summary*") And c.Row > 1
        Set c = c.Offset(-1, 0)
    Loop

    'need to switch freeze location?
    If c.Row > 1 And c.Row <> lastFreeze Then
        ActiveWindow.FreezePanes = False
        'prevent re-triggering event
        Application.EnableEvents = False
        Application.GoTo c.Offset(-1, 0), True
        c.Offset(1, 0).Select
        ActiveWindow.FreezePanes = True
        sel.Select
        Application.EnableEvents = True
        lastFreeze = c.Row
    End If

End Sub
Run Code Online (Sandbox Code Playgroud)