通常解冻窗格的代码是
ActiveWindow.FreezePanes = False
但是假设我有 Workbook 类型的 sourceBook 和 String 类型的 sheetName。例如
sourceBook.sheets(sheetName)
如何在不调用 ActiveWindow 之类的东西的情况下解冻该工作簿工作表上的窗格?
这将从给定工作簿的每个窗口的每个工作表中删除 FreezePanes,它还确保与运行代码之前相同的工作表处于活动状态:
Dim w As Window
Dim activews As Worksheet, ws As Worksheet
For Each w In sourceBook.Windows
w.Activate
If activews Is Nothing Then
Set activews = w.ActiveSheet
End If
For Each wsv In w.SheetViews
wsv.Sheet.Activate
w.FreezePanes = False
Next
activews.Activate
set activews = nothing
Next
正如 Rick 所指出的,您不能在不激活窗口的情况下解冻窗格。但你不必打电话ActiveWindow
。
Unfortunately Freeze Panes is a method of a window object, the sheet on which you want to apply it needs to be the activesheet. This means that you will have to select the cell on which you would like to freeze the panes on the active window only.
将代码添加到工作表的激活事件(例如,这是 Sheet1 下的代码)
Option Explicit
Private Sub Worksheet_Activate()
ActiveWindow.FreezePanes = False
End Sub