excel利用VBA设置或取消打印区域
时间:2023-08-03 16:21:58
如果需要用VBA代码在Excel工作表中设置打印区域,可以用下面的方法。
一、设置打印区域
方法一:
用PageSetup.PrintArea属性。代码如下:
Sub SetPrintArea()
Sheet1.PageSetup.PrintArea = "A1:F15"
End Sub
上述代码将区域A1:F15设置为打印区域。如果要同时设置多个打印区域,可以将上述代码改为:
Sub SetPrintArea()
Sheet1.PageSetup.PrintArea = "A1:F15,A20:F45"
End Sub
上述代码将区域A1:F15及A20:F45设置为打印区域,可以分两页来打印。
还可以使用变量来设置一个动态打印区域:
Sub SetPrintArea()
PrintRow = 100
Sheet1.PageSetup.PrintArea = "A1:G" & PrintRow
End Sub
通过改变变量PrintRow的值来设置不同的打印区域。
方法二:
用Names.Add方法。当在Excel中设置一个区域为打印区域后,Excel自动定义了一个名称“Print_Area",因而可以通过定义名称的方法来设置打印区域,代码如下:
Sub SetPrintArea()
Sheet1.Names.Add "Print_Area", Sheet1.Range("B2:G12")
End Sub
设置动态打印区域:
Sub SetPrintArea()
Sheet1.Names.Add "Print_Area", Sheet1.Range("B2").Resize(12, 8)
End Sub
上述代码将B2:I13区域设置为打印区域,通过改变Resize(12, 8)的参数来调整打印区域的大小。
二、取消打印区域
将PageSetup.PrintArea属性设置为空:
Sub ClearPrintArea()
Sheet1.PageSetup.PrintArea = "" '空字符串
End Sub
或删除已定义的“Print_Area”名称:
Sub ClearPrintArea()
On Error Resume Next
Sheet1.Names("Print_Area").Delete
On Error GoTo 0
End Sub
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
两台未联网的Win7电脑怎么建立局域网游戏?
![](https://img.aspxhome.com/file/2023/27/a231351_0s.jpg)
苹果iOS系统是及时更新好还是等稳定了再更新好?
![](https://img.aspxhome.com/file/2023/1/a196151_0s.jpg)
Win10 KB5000802更新后电脑打印机崩溃蓝屏怎么办?
![](https://img.aspxhome.com/file/2023/26/a222531_0s.jpg)
微软计划为Win10锁屏界面添加搜索框
![](https://img.aspxhome.com/file/2023/3/a299549_0s.jpg)
Word文档中的重复字符串自动更新
Win10无法运行文明5提示0xc0000142的解决方法
![](https://img.aspxhome.com/file/2023/4/a305875_0s.png)
WPS文字中如何利用公式编辑器制作图形标注
![](https://img.aspxhome.com/file/2023/7/a167177_0s.png)
Win11怎么设置显卡直连?
![](https://img.aspxhome.com/file/2023/27/a236157_0s.png)
Win11系统怎么开启telnet服务?Win11开启telnet服务方法
![](https://img.aspxhome.com/file/2023/1/a274424_0s.jpg)
微软承诺:致力于解决 Win11 应用兼容性问题
![](https://img.aspxhome.com/file/2023/28/a240763_0s.jpg)
如何使用金山wps给文字添加空行
![](https://img.aspxhome.com/file/2023/4/a163944_0s.png)
wps表格如何批量为数字添加负号
![](https://img.aspxhome.com/file/2023/9/53589_0s.jpg)
Windows10系统彻底关闭Windows Defender的技巧分享!
![](https://img.aspxhome.com/file/2023/30/a268987_0s.jpg)
Win 10移动版暗藏售店演示模式 开启很简单
![](https://img.aspxhome.com/file/2023/28/a248134_0s.jpg)
PPT 2019中对文本框样式进行设置的方法图解教程
![](https://img.aspxhome.com/file/2023/10/a353105_0s.jpg)
苹果Mac通知中心怎么用?Mac通知栏设置使用技巧介绍
![](https://img.aspxhome.com/file/2023/7/a214967_0s.png)
Windows Storage Server 2008 (x64)BT下载
外语辅助,就选它们!
![](https://img.aspxhome.com/file/2023/8/a209548_0s.jpeg)
windows未能启动怎么办电脑无法启动windows解决方法
![](https://img.aspxhome.com/file/2023/2/a287093_0s.jpg)
租号玩APP怎么绑定支付宝?租号玩APP绑定支付宝的方法
![](https://img.aspxhome.com/file/2023/8/a333348_0s.jpg)