VBA Excel - 在页面布局视图中转到下一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8997761/
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 15:02:40  来源:igfitidea点击:

VBA Excel - go to next page in page layout view

excelvbapage-layout

提问by simchuck

I am copying various ranges to a new Excel sheet, and looking for a solution to referencing the next page, or any specific page, while in Page Layout view.

我正在将各种范围复制到新的 Excel 工作表中,并在页面布局视图中寻找引用下一页或任何特定页面的解决方案。

I have already setup the page layout with margins, headers, and other formatting, and want to fill in my report based on the layout presented on screen. Since the layout is set, I can hard code the cell references to place my ranges, but I would rather determine this dynamically. Any solutions out there?

我已经设置了带有边距、标题和其他格式的页面布局,并希望根据屏幕上显示的布局填写我的报告。由于布局已设置,我可以对单元格引用进行硬编码以放置我的范围,但我宁愿动态确定这一点。有什么解决办法吗?

回答by Ashton Sheets

The easiest way to do this is to change to Page Layout view, then to use LargeScrollto go down to which ever page you want.

最简单的方法是切换到页面布局视图,然后使用LargeScroll转到您想要的页面。

ActiveWindow.View = xlPageLayoutView     '<--- Changes view to "Page Layout"
ActiveWindow.LargeScroll 1               '<--- Scrolls down a full page 1 time

The '1' is the number of LargeScrolls you want to execute so in the example above, you'd go down 1 page from wherever you are. This will work from any page in Page Layout View.

'1' 是您要执行的 LargeScrolls 的数量,因此在上面的示例中,无论您身在何处,都可以向下移动 1 页。这适用于页面布局视图中的任何页面。

Here's an example for if you wanted to go to page 2 but were unsure what page your code left you on. It uses cells(1,1)to take you to the first cell of the worksheet which will be page 1.

这是一个示例,如果您想转到第 2 页但不确定您的代码将您留在哪个页面。它用于cells(1,1)将您带到工作表的第一个单元格,即第 1 页。

Cells(1,1).Activate                      '<--- Takes you to first cell in your worksheet
ActiveWindow.View = xlPageLayoutView     
ActiveWindow.LargeScroll 1

You can change the '1' to any number. Remember, it works like offset, so if you start in cell A1 and want to go to page 3, you would only scroll 2 times, not 3. The code would look like ActiveWindow.LargeScroll 2because it's taking you down 2 from current page (2 + 1).

您可以将“1”更改为任何数字。请记住,它的工作原理类似于偏移量,因此如果您从单元格 A1 开始并想要转到第 3 页,您将只滚动 2 次,而不是 3。代码看起来像ActiveWindow.LargeScroll 2因为它使您从当前页面 (2 + 1 )。

回答by Joseph

This is a tough one, but one suggestion could be that if you already have the layout set, then consider it your template page. There is a trick you can use to figure out if you'll "fall out of range," so-to-say. Before copying a new range to the template sheet, determine the height of the content you're copying and hold it against the "left-over" height of the destination (where the page will break). If it falls out of range, move it to the next page so you don't break up your ranges by the pages.

这是一个艰难的过程,但一个建议可能是,如果您已经设置了布局,则将其视为您的模板页面。有一个技巧可以用来判断您是否会“超出范围”,可以这么说。在将新范围复制到模板工作表之前,确定您要复制的内容的高度并将其保持在目标的“左侧”高度(页面将中断的位置)。如果超出范围,请将其移至下一页,以免按页面划分范围。

How would you know if it falls out of range? You can figure out a standard height per page when you start your code. Then decrement it as you paste on. This way will take care of different row heights you may have when copying/pasting.

你怎么知道它是否超出范围?您可以在开始编写代码时计算出每页的标准高度。然后在粘贴时递减它。这种方式将处理复制/粘贴时可能具有的不同行高。

To figure out the height, when you select the range in code, just check it's Height property (Range("A1").Height) and it will let you know where the next range's Top property will lay. Also, you could hard code the standard height (just highlight the cells that fit on one page and go to the immediate window and type ?Selection.Height and you'll have your standard height to work with).

要计算高度,当您在代码中选择范围时,只需检查它的 Height 属性(Range("A1").Height),它就会让您知道下一个范围的 Top 属性将位于何处。此外,您可以对标准高度进行硬编码(只需突出显示适合一页的单元格,然后转到直接窗口并输入 ?Selection.Height ,您就可以使用标准高度)。

Hope this helps!

希望这可以帮助!