gpt4 book ai didi

vba - 使用 VBA 将范围添加到现有 PrintArea 失败并出现错误 400

转载 作者:行者123 更新时间:2023-12-04 21:21:42 24 4
gpt4 key购买 nike

我正在尝试扩展我的 Excel 工作表的当前打印区域,但目前使用以下代码。它在第三个 .PageSetup.PrintArea 处中断我不知道为什么。

编码:

With ActiveSheet
.PageSetup.PrintArea = "$A$1:$A$5"

MsgBox (.PageSetup.PrintArea & "," & .Cells(8, 1).Address & ":" & .Cells(10, 1).Address)
.PageSetup.PrintArea = .PageSetup.PrintArea & "," & .Cells(8, 1).Address & ":" & .Cells(10, 1).Address

MsgBox (.PageSetup.PrintArea & "," & .Cells(15, 1).Address & ":" & .Cells(20, 1).Address)
.PageSetup.PrintArea = .PageSetup.PrintArea & "," & .Cells(15, 1).Address & ":" & .Cells(20, 1).Address
End With

在最后一行,它给我一个“错误 400”,我不知道为什么,它与上面的行完全相同。我也试过 ;而不是 ,但它不明白。如果您有任何想法,请告诉我,我会很高兴听到它。
谢谢,克莱门特

最佳答案

使用.Union (Application.Union Method):

.PageSetup.PrintArea = Union(.Range("$A$1:$A$5"), .Range(.Cells(8, 1), .Cells(10, 1)), .Range(.Cells(15, 1), .Cells(20, 1))).Address

加入所有 3 个范围。

或使用
.PageSetup.PrintArea = Union(.Range("Print_Area"), .Range(.Cells(8, 1), .Cells(10, 1))).Address

将范围添加到现有打印区域。
这里我们使用现有打印区域保存为命名范围 Print_Area的事实。我们可以通过 .Range("Print_Area") 访问.

In case anoyne is suspect of how the English name Print_Area would behave in localized Office versions, we won't run into any issues because Print_Area always works even if the name manager shows the localized name of Print_Area.

关于vba - 使用 VBA 将范围添加到现有 PrintArea 失败并出现错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43975004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com