小编Edg*_*ard的帖子

如何"强制"Excel正确计算分页符?

我应该将一些大型数据范围从Excel导出到Powerpoint,每张幻灯片一页,当然我应该处理分页符以避免"孤立"行或列.

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

当我逐步调试我的代码时,它运行良好,逻辑似乎没问题,但如果我"自由"运行它,页面中断完全关闭.添加一些MsgBox指令,我可以看到当我读取HPageBreaks.Count(或垂直)时,我得到了错误的值.(如果我手动执行代码应该做什么,我可以检查正确的).

在许多论坛上搜索,我看到一些丑陋的变通方法,比如强制重置PaperSize(ws.PageSetup.PaperSize = ws.PageSetup.PaperSize).在尝试了其中一些之后,似乎工作得更好的是在更改为PageSetup之前关闭PrintCommunication,然后将其重新打开.这在我的大多数床单上运作良好,但是在非常大的(约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 = …
Run Code Online (Sandbox Code Playgroud)

excel vba excel-vba

8
推荐指数
1
解决办法
1348
查看次数

标签 统计

excel ×1

excel-vba ×1

vba ×1