VBA Excel - 转到页面布局视图中的下一页
我正在将各种范围复制到新的 Excel 工作表,并寻找在页面布局视图中引用下一页或任何特定页面的解决方案。
我已经设置了包含边距、标题和其他格式的页面布局,并希望根据屏幕上显示的布局填写我的报告。由于布局已设置,我可以对单元格引用进行硬编码以放置我的范围,但我宁愿动态确定这一点。有什么解决方案吗?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是更改为页面布局视图,然后使用
LargeScroll
转到您想要的页面。“1”是您要执行的 LargeScrolls 的数量,因此在上面的示例中,无论您所在的位置,您都会向下移动 1 页。这适用于页面布局视图中的任何页面。
下面是一个示例,说明您是否想转到第 2 页,但不确定代码将您留在哪个页面。它使用
cells(1,1)
将您带到工作表的第一个单元格,即第 1 页。您可以将“1”更改为任何数字。请记住,它的工作原理类似于偏移量,因此如果您从单元格 A1 开始并想要转到第 3 页,则只需滚动 2 次,而不是 3 次。代码看起来像
ActiveWindow.LargeScroll 2
因为它是使您从当前页面下降 2 (2 + 1)。The easiest way to do this is to change to Page Layout view, then to use
LargeScroll
to go down to which ever page you want.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.
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.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 2
because it's taking you down 2 from current page (2 + 1).这是一项艰巨的任务,但一个建议可能是,如果您已经设置了布局,则将其视为您的模板页面。有一个技巧可以用来判断你是否会“超出范围”,可以这么说。在将新范围复制到模板表之前,确定要复制的内容的高度并将其与目标的“剩余”高度(页面将断开的位置)保持一致。如果超出范围,请将其移至下一页,这样就不会按页打乱范围。
你怎么知道它是否超出范围?当您开始编写代码时,您可以计算出每页的标准高度。然后在粘贴时递减它。这种方式将处理复制/粘贴时可能出现的不同行高。
要计算高度,当您在代码中选择范围时,只需检查它的 Height 属性 (Range("A1").Height),它会让您知道下一个范围的 Top 属性的位置。另外,您可以对标准高度进行硬编码(只需突出显示适合一页的单元格,然后转到直接窗口并输入 ?Selection.Height,您将获得可使用的标准高度)。
希望这有帮助!
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).
Hope this helps!