Excel:如何以编程方式检索Worksheet的冻结范围?

时间:2015-05-15 15:31:31

标签: vb.net excel range vsto freeze

我正在使用VSTO构建Excel加载项。

我想构建两个函数。第一个,将冻结范围存储在名为RNG的Excel.Range变量中,然后使用以下命令解冻窗格。

Globals.ThisAddIn.Application.ActiveWindow.FreezePanes = False

第二个功能选择范围并再次冻结。有了以下

RNG.Select()
Globals.ThisAddIn.Application.ActiveWindow.FreezePanes = True

我不知道如何在解冻窗户之前存放冰冻范围。

有人可以帮我这样做,还是知道其他一些解决方法?

感谢。

1 个答案:

答案 0 :(得分:0)

在@Byron的帮助下,我解决了我的问题。这是我的代码!

'Flag that indicates if there is a "frozen" scenario stored in the other variables
Private frozen_scenario As Boolean
'Range that marks the first cell of the frozen header
Private range_freeze_begin As Excel.Range
'Range that marks the the first cell not contained by the frozen header
Private range_freeze_end As Excel.Range
'Range that marks the the first visible cell (in the not fixed pane)
Private first_visible_cell_not_fixed As Excel.Range

'Unfreezes the panes, saving the current scenario
Private Sub unfreezeLines()
    With Globals.ThisAddIn.Application.ActiveWindow
        If .FreezePanes Then
            Dim frozen_pane_limit_line As Integer
            Dim frozen_pane_limit_column As Integer

            frozen_pane_limit_line = .Panes(1).VisibleRange.Rows.Count + 1
            frozen_pane_limit_column = .Panes(1).VisibleRange.Columns.Count + 1

            If .Panes.Count = 2 Then
                If .Panes(1).VisibleRange(1, 1).Row = .Panes(2).VisibleRange(1, 1).Row Then
                    frozen_pane_limit_line = 1
                Else
                    frozen_pane_limit_column = 1
                End If

                Me.first_visible_cell_not_fixed = .Panes(2).VisibleRange(1, 1)
            Else '4 panes
                Me.first_visible_cell_not_fixed = .Panes(4).VisibleRange(1, 1)
            End If

            Me.range_freeze_begin = .Panes(1).VisibleRange(1, 1)
            Me.range_freeze_end = Me.sheet.Cells(frozen_pane_limit_line, frozen_pane_limit_column)
            Me.frozen_scenario = True

            .FreezePanes = False
        End If
    End With
End Sub

'Recovers the frozen state, exactly like it was when the first function was called
Private Sub recuperaLinhasCongeladas()
    If Me.frozen_scenario Then
        'Creating the frozen header again
        Globals.ThisAddIn.Application.Goto(Me.range_freeze_begin, True)
        Me.range_freeze_end.Select()
        Globals.ThisAddIn.Application.ActiveWindow.FreezePanes = True

        'Showing the same cell at the top
        Globals.ThisAddIn.Application.Goto(Me.first_visible_cell_not_fixed, True)

        Me.frozen_scenario = False
    End If
End Sub