excel利用VBA在页眉页脚中调用单元格内容
时间:2023-09-24 12:57:06
有时我们需要在Excel的页眉页脚中调用某个单元格内容,例如让用户在A1单元格中填入月份,如“一月”,在页眉处自动生成“一月报表”,可以用VBA代码来实现,步骤如下:
1.按Alt+F11,打开VBA编辑器。
2.在“工程”窗口中,双击“ThisWorkbook”,在右侧的代码窗口中输入下列代码:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
ActiveSheet.PageSetup.LeftHeader = Range("A1").Value & "报表"
End Sub
这样,每次在打印工作表时,会自动在页眉左侧调用A1单元格中的内容,如A1单元格内容为“一月”,左侧页眉中显示“一月报表”。
如果要在页脚中调用单元格内容,将上述代码改为:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
ActiveSheet.PageSetup.LeftFooter = Range("A1").Value & "报表"
End Sub
同样,如果需要在页眉或页脚的中部、右侧调用单元格内容,只须将“LeftHeader”更换成“CenterHeader”、“CenterFooter”、“RightHeader” 和 “RightFooter”。
标签:excel,利用,VBA,在,页眉,页脚,中,调用,单元格
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Win10系统无法设置移动热点怎么办?Win10系统解决无法设置移动热点的方法
2023-12-13 09:16:46
![](https://img.aspxhome.com/file/2023/7/52827_0s.png)
PDFBinder怎么将多个pdf文件合并为一个?
2023-08-29 01:46:39
![](https://img.aspxhome.com/file/2023/7/15847_0s.jpg)
Excel如何制作个性单元格
2023-06-08 12:51:48
![](https://img.aspxhome.com/file/2023/6/36346_0s.jpg)
Windows Server 2008添加Hyper-V组件
2023-08-03 16:27:06
![](https://img.aspxhome.com/file/2023/29/a259645_0s.jpg)
Windows错误报告占用CPU该怎么解决?
2023-01-02 05:43:49
![](https://img.aspxhome.com/file/2023/6/a322772_0s.png)
Win10玩游戏出现蓝屏提示“win32kfull.sys”的解决方法
2023-07-09 14:42:11
![](https://img.aspxhome.com/file/2023/1/a281353_0s.png)
Windows10出现“你需要权限才能执行此操作”提示的处理方法
2023-03-22 01:35:12
![](https://img.aspxhome.com/file/2023/26/a224217_0s.jpg)
WPS Excel中如何提取工作表名称作为标题
2022-09-05 06:25:35
![](https://img.aspxhome.com/file/2023/9/a186049_0s.jpg)
Win7任务栏预览窗口显示太慢的解决方法
2022-07-05 12:34:02
![](https://img.aspxhome.com/file/2023/27/a232960_0s.jpg)
微软将于今年10月停止对Win10 1703的更新
2023-03-22 08:03:24
![](https://img.aspxhome.com/file/2023/4/a306944_0s.jpg)
如何设置word单元格文字居中
2023-08-27 03:04:36
![](https://img.aspxhome.com/file/2023/3/34993_0s.png)
电脑如何设置一键锁屏?
2023-08-26 12:03:48
![](https://img.aspxhome.com/file/2023/1/a273051_0s.jpg)
Excel怎么使用isna函数判断数据?
2023-12-03 17:38:49
![](https://img.aspxhome.com/file/2023/3/36403_0s.jpg)
wps表格怎么制作柱形图
2023-07-04 08:28:02
如何防止Mac记住已加入的Wi-Fi网络?
2022-04-30 23:58:12
![](https://img.aspxhome.com/file/2023/6/a204586_0s.jpeg)
在Word2016中输入日期和时间的方法有哪些
2023-01-29 07:05:59
excel如何使用记忆功能
2022-07-09 03:46:00
PPT插入、复制和移动幻灯片本节的操作方法
2023-09-29 04:23:29
![](https://img.aspxhome.com/file/2023/10/a351737_0s.jpg)
电脑提示:磁盘上没有足够的空间完成此操作怎么办?
2022-03-01 08:00:37
![](https://img.aspxhome.com/file/2023/25/a218580_0s.png)
win8.1系统禁用IE11浏览器URL输入联想功能的方法图文教程
2023-02-01 09:44:22
![](https://img.aspxhome.com/file/2023/28/a243766_0s.png)