使用VBA来设置Excel的页眉页脚、操作页面设置、控制Excel打印,对于灵活性大、数据多、数据表复杂的工作薄,相当有效,下面,给您介绍些本站收集总结的精典代码!
①VBA设置页眉页脚
Dim EachSheet As Worksheet
For Each EachSheet In ThisWorkbook.Sheets
EachSheet.PageSetup.RightFooter = "页脚文字插入当前日期:" & Format(Date, "yyyy年m月d日")
EachSheet.PageSetup.LeftHeader = "页眉标题文字左对齐"
EachSheet.PageSetup.CenterHeader = "页眉标题文字居中对齐"
Next EachSheet
②VBA操作页面设置
Dim EachSheet As Worksheet
With EachSheet
.PageSetup.LeftMargin = 10 '左边距
.PageSetup.RightMargin = 10 '右边距
.PageSetup.TopMargin = 25 '上边距
.PageSetup.BottomMargin = 10 '下边距
.PageSetup.HeaderMargin = Application.InchesToPoints(0.290551181102362) '页眉
.PageSetup.FooterMargin = Application.InchesToPoints(0.290551181102362) '页脚
.PageSetup.CenterHorizontally = True '是否垂直居中,'打印页面是否水平居中
.PageSetup.CenterVertically = False
.PageSetup.Orientation = 2 '设置打印方向:1为纵向打印,2为横向打印
.PageSetup.PaperSize = 9 '设置打印纸的类型,例如A3为8,A4为9
.PageSetup.Zoom = 43 '设置页面缩放的百分比
.PrintPreview '打印预览
.PrintOut '直接打印
.PrintOut Copies:=1, Collate:=True '双面打印
End With