2015-05-15 3 views
0

Я использую VSTO для создания надстройки Excel.Excel: Как программно получить замороженный диапазон рабочего листа?

Я хочу построить две функции. Первый, сохраняет замороженный диапазон в моей переменной Excel.Range, называемой RNG, а затем размораживает панели, используя следующую команду.

Globals.ThisAddIn.Application.ActiveWindow.FreezePanes = False 

Вторая функция выбирает диапазон и снова замораживает его. Со следующими

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

Что я не знаю, как хранить замороженный диапазон перед тем, как разморозить окно.

Помогает ли кто-нибудь мне в этом, или знает какое-то другое обходное решение?

Спасибо.

+0

Взгляните на это [родственный вопрос] (http://stackoverflow.com/questions/30242193/vba-insert-rows-below-a-dynamic-header/30242582#30242582). Он дает код для извлечения 'VisibleRange'. –

+0

Perfect. Спасибо! –

+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 
+0

Извините за плохой английский ... –

Смежные вопросы