如何“强制”Excel来正确计算分页符?

我应该从Excel导出一些大的数据范围到Powerpoint,每张幻灯片一页,当然,我应该把分页符,以避免“孤儿”行或列。

我正在尝试通过读取HPageBreaks.Count和VPageBreaks.Count来检查具有给定缩放的垂直和水平多less页,然后手动定义每个中断的位置。 这个想法是在每个页面上具有大致相同的宽度和高度。

当我一步一步地debugging我的代码时,它运行的很好,逻辑看起来不错,但是如果我“自由地”运行它,分页符就完全closures了。 添加一些MsgBox指令,我可以看到,当我读HPageBreaks.Count(或垂直)时,我得到了错误的值。 (如果我手动执行代码,我可以检查正确的代码)。

search许多论坛,我看到一些丑陋的解决方法,如强制重置PaperSize(ws.PageSetup.PaperSize = ws.PageSetup.PaperSize)。 在尝试了一些之后,看起来效果更好的是在更改PageSetup之前closuresPrintCommunication,然后再打开它。 这在我的大多数表单上运行良好,但是在真正大的表单上(〜750行×80列,几乎所有的表单都是公式),它根本就不是。

这里的代码摘录如下:

'Reset page breaks .ResetAllPageBreaks 'Set minimum acceptable zoom factor Application.PrintCommunication = False 'This is the ugly workaround .PageSetup.Zoom = 60 Application.PrintCommunication = True MsgBox "Zoom = " & .PageSetup.Zoom 'Only for debugging 'Calculate the number of pages in width Application.PrintCommunication = False NPagesWide = .VPageBreaks.Count + 1 Application.PrintCommunication = True MsgBox "NPagesWide = " & NPagesWide 'Find the higher zoom factor that can fit that number of pages Application.PrintCommunication = False .PageSetup.Zoom = 100 Application.PrintCommunication = True Do While .VPageBreaks.Count > NPagesWide - 1 Application.PrintCommunication = False .PageSetup.Zoom = .PageSetup.Zoom - 5 Application.PrintCommunication = True Loop MsgBox "Zoom = " & .PageSetup.Zoom 'Set average width per page and initialize column pointer If HasTitleColumns Then 'Defined earlier PageWidth = (PrintArea.Width + TitleColumns.Width * (NPagesWide - 1)) / NPagesWide j = TitleColumns.Columns(TitleColumns.Columns.Count).Column + 1 Else PageWidth = PrintArea.Width / NPagesWide j = 1 End If 'Cycle vertical page breaks For i = 1 To NPagesWide - 1 'Set width of TitleColumns If HasTitleColumns Then CumulWidth = TitleColumns.Width Else CumulWidth = 0 End If 'Cumulate columns width until the available page width Do While CumulWidth + .Columns(j).Width <= PageWidth CumulWidth = CumulWidth + .Columns(j).Width j = j + 1 Loop 'Add the break .VPageBreaks.Add .Columns(j + 1) Next i 

任何想法为什么发生这种情况,我该如何解决?

谢谢,

我提出一般性的build议,当VBA代码在debugging模式下按F8的时候工作的很好,但是在按F5运行整个macros之后它不会工作

提示1.尽可能使用ThisWorkbook.ActiveSheet而不是ActiveWorkbook.ActiveSheet来引用正确的工作表。 使用ThisWorkbook.Application而不是仅Application. 这可能是因为你有另一个Addin程序在后台工作,把ActiveSheet切换到其他你可能不知道的东西。 检查其他macros启用并摆脱任何你不使用。 因此,在代码中任何重要之前,请尝试使用ThisWorkbook.Activate获取工作表的焦点。 查看该页面上的图表: http : //analystcave.com/vba-tip-day-activeworkbook-vs-thisworkbook/

提示2.强制Excel以不同的方式等待DoEvents

 Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'Place this line of code at the very top of module Sleep 1 'This will pause execution of your program for 1 ms 

https://stackoverflow.com/a/3891017/1903793 https://www.fmsinc.com/microsoftaccess/modules/examples/AvoidDoEvents.asp

或者:

 Application.Calculate If Not Application.CalculationState = xlDone Then DoEvents End If 

https://stackoverflow.com/a/11277152/1903793

提示3.如果以上仍不起作用,

 ThisWorkbook.Connections("ConectionName").Refresh ThisWorkbook.Application.CalculateUntilAsyncQueriesDone 

https://stackoverflow.com/a/26780134/1903793