如何在 Excel 2007 VBA 中以编程方式冻结 Excel 工作表的顶行?
我希望通过 VBA 以编程方式冻结 Excel 工作表的顶行。最终目标是产生与 View > 相同的效果。冻结窗格> Excel 2007 中的“冻结顶行”命令可以冻结工作表的顶行,用户即使在滚动数据时也可以看到工作表的顶行。
I am looking to programmatically freeze the top row of an Excel worksheet from VBA. The end goal is to produce the same effect as the View > Freeze Panes > Freeze Top Row
command in Excel 2007 so that the top row of the worksheet is frozen and users can see the top row of the worksheet even as they scroll through the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
选择不同的范围以获得不同的效果,与手动操作的方式非常相似。 “冻结顶行”实际上只是 Excel 2007(及更高版本)中新增的快捷方式,与早期版本的 Excel 相比,它不包含任何附加功能。
Select a different range for a different effect, much the same way you would do manually. The "Freeze Top Row" really just is a shortcut new in Excel 2007 (and up), it contains no added functionality compared to earlier versions of Excel.
Tomalak 已经给了您正确的答案,但我想补充一点,大多数时候,当您想知道在用户界面中执行特定操作所需的 VBA 代码时,记录宏是一个好主意。
在这种情况下,单击功能区开发人员选项卡上的“录制宏”,冻结顶行,然后停止录制。 Excel 将为您录制以下宏,它也可以完成这项工作:
Tomalak already gave you a correct answer, but I would like to add that most of the times when you would like to know the VBA code needed to do a certain action in the user interface it is a good idea to record a macro.
In this case click Record Macro on the developer tab of the Ribbon, freeze the top row and then stop recording. Excel will have the following macro recorded for you which also does the job:
录制的宏的问题与内置操作的问题相同:Excel 选择冻结顶部可见行,而不是可以找到标题信息的实际顶行。
在这种情况下,宏的目的是冻结实际的顶行。当我查看行 #405592 时,我需要检查该列的标题(因为我在打开文件时忘记冻结行),我必须滚动到顶部,冻结顶行,然后找到返回的方式再次行#405592。因为我认为这是愚蠢的行为,所以我想要一个宏来纠正它,但是,就像我说的,录制的宏只是模仿同样的愚蠢行为。
我正在使用 Office 2011 for Mac OS X Lion
更新(2 分钟后):
我在这里找到了解决方案:http ://www.ozgrid.com/forum/showthread.php?t=19692
The problem with the recorded macro is the same as the problem with the built-in action: Excel chooses to freeze the top visible row, rather than the actual top row where the header information can be found.
The purpose of a macro in this case is to freeze the actual top row. When I am viewing row #405592 and I need to check the header for the column (because I forgot to freeze rows when I opened the file), I have to scroll to the top, freeze the top row, then find my way back to row #405592 again. Since I believe this is stupid behavior, I want a macro to correct it, but, like I said, the recorded macro just mimics the same stupid behavior.
I am using Office 2011 for Mac OS X Lion
Update (2 minutes later):
I found a solution here: http://www.ozgrid.com/forum/showthread.php?t=19692
刚刚遇到同样的问题...
由于某种原因,freezepanes 命令只会导致十字准线出现在屏幕中央。原来我已经关闭了 ScreenUpdating!
使用以下代码解决:
现在工作正常。
Just hit the same problem...
For some reason, the freezepanes command just caused crosshairs to appear in the centre of the screen. It turns oout I had switched ScreenUpdating off!
Solved with the following code:
Now it works fine.
将这个问题扩展到 Excel 之外的使用领域
自己的 VBA,ActiveWindow 属性 必须作为 Excel.Application 对象 的子对象进行寻址。
从 Access 创建 Excel 工作簿的示例:
核心过程实际上只是重复以前提交的答案,但我认为演示当您不在 Excel 自己的 VBA 中时如何处理 ActiveWindow 很重要。虽然这里的代码是VBA,但它应该可以直接转录为其他语言和平台。
To expand this question into the realm of use outside of Excel
s own VBA, the ActiveWindow property must be addressed as a child of the Excel.Application object.
Example for creating an Excel workbook from Access:
The core process is really just a reiteration of previously submitted answers but I thought it was important to demonstrate how to deal with ActiveWindow when you are not within Excel's own VBA. While the code here is VBA, it should be directly transcribable to other languages and platforms.
这是冻结顶行的最简单方法。
FreezePanes
的规则是它将冻结您所选单元格的左上角。例如,如果您突出显示 C10,它将冻结在 B 列和 C 列、第 9 行和第 10 行之间。因此,当您突出显示第 2 行时,它实际上冻结在第 1 行和第 2 行(顶行)之间。另外,一旦您解冻窗口,
.SplitColumn
或.SplitRow
就会分割您的窗口,这不是我喜欢的方式。This is the easiest way to freeze the top row. The rule for
FreezePanes
is it will freeze the upper left corner from the cell you selected. For example, if you highlight C10, it will freeze between columns B and C, rows 9 and 10. So when you highlight Row 2, it actually freeze between Rows 1 and 2 which is the top row.Also, the
.SplitColumn
or.SplitRow
will split your window once you unfreeze it which is not the way I like.