Excel VBA:解冻工作簿对象工作表的窗格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13752987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:43:53  来源:igfitidea点击:

Excel VBA: Unfreezing panes of a workbook object's sheet?

excelvba

提问by KaliMa

Normally the code to unfreeze panes is

通常解冻窗格的代码是

ActiveWindow.FreezePanes = False

But say I have sourceBook of type Workbook and a sheetName of type String. For instance

但是假设我有 Workbook 类型的 sourceBook 和 String 类型的 sheetName。例如

sourceBook.sheets(sheetName)

How do I unfreeze the panes on that workbook's sheet without invoking things like ActiveWindow?

如何在不调用 ActiveWindow 之类的东西的情况下解冻该工作簿工作表上的窗格?

采纳答案by Daniel

This will remove FreezePanes from every sheet for every window for the given workbook, it also makes sure the same worksheet is active as was active before running the code:

这将从给定工作簿的每个窗口的每个工作表中删除 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

As Rick indicated, you cannot unfreeze panes without activating the window. But you don't have to call ActiveWindow.

正如 Rick 所指出的,您不能在不激活窗口的情况下解冻窗格。但是您不必调用ActiveWindow.

回答by Rick

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.

不幸的是,冻结窗格是窗口对象的一种方法,您要应用它的工作表必须是活动工作表。这意味着您必须仅选择要冻结活动窗口上窗格的单元格。

回答by shahkalpesh

Add the code to sheet's activate event (for e.g. this is the code under Sheet1)

将代码添加到工作表的激活事件(例如这是工作表 1 下的代码)

Option Explicit
Private Sub Worksheet_Activate()
    ActiveWindow.FreezePanes = False
End Sub