Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 5011|回复: 15

[已解决]批量设置页面格式

[复制链接]
发表于 2016-12-21 09:53 | 显示全部楼层 |阅读模式
本帖最后由 dyzx 于 2016-12-21 16:31 编辑

各位老师:请问怎样统一设置页面格式(将7班、8班....等N个班统一设置打印页面格式,代码怎样写,请指教,多谢。
最佳答案
2016-12-21 16:02
  1. Sub 页面设置3()
  2.     'tt = Timer
  3.     Dim sh As Worksheet
  4.     Application.PrintCommunication = False
  5.     For Each sh In Worksheets
  6.          If sh.Name Like "*班" Then x = x & "," & sh.Name
  7.     Next
  8.     x = Mid(x, 2)
  9.     If InStr(x, ",") > 0 Then Sheets(Split(x, ",")).Select Else If Len(x) > 0 Then Sheets(x).Select
  10.     Sheets(Split(x, ",")(0)).Activate
  11.     With ActiveSheet.PageSetup
  12.         .Orientation = xlLandscape  '纸张横向'
  13.         .Orientation = xlPortrait  '纸张纵向'
  14.         .PaperSize = xlPaperA4
  15.         .LeftMargin = Application.CentimetersToPoints(0.1)
  16.         .RightMargin = Application.CentimetersToPoints(0.1)
  17.         .TopMargin = Application.CentimetersToPoints(1)
  18.         .BottomMargin = Application.CentimetersToPoints(1)
  19.         .HeaderMargin = Application.CentimetersToPoints(1)
  20.         .FooterMargin = Application.CentimetersToPoints(0.5)
  21.         .BlackAndWhite = True  '单色打印
  22.         .PrintTitleRows = "$1:$2" '设置“顶端标题行”
  23.         .CenterHorizontally = True  '水平居中
  24.         .LeftFooter = "第 &P 页,共 &N 页"
  25.     End With
  26.     Application.PrintCommunication = True
  27.     'MsgBox Timer - tt
  28. End Sub
复制代码

试场编排00.rar

23.66 KB, 下载次数: 27

发表于 2016-12-21 10:09 | 显示全部楼层
把全部工作表同时选上后手动打印设置。
也可以设置一个模板,打印前往里填数据。
回复

使用道具 举报

 楼主| 发表于 2016-12-21 10:30 | 显示全部楼层
grf1973 发表于 2016-12-21 10:09
把全部工作表同时选上后手动打印设置。
也可以设置一个模板,打印前往里填数据。

grf1973老师:如果工作表很多,我想用代码全部选定工作表,这句代码应该怎样写,多谢指教。
回复

使用道具 举报

发表于 2016-12-21 10:37 | 显示全部楼层
  1. Sub 选择所有工作表()
  2.     Dim sh As Worksheet
  3.     For Each sh In Worksheets
  4.         x = x & "," & sh.Name
  5.     Next
  6.     Sheets(Split(Mid(x, 2), ",")).Select
  7. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-12-21 10:48 | 显示全部楼层

grf1973老师:如果要选择的工作表是有固定形式(如我附件中的是:1班、2班、3班....等N个表),你的代码都适用吗?
回复

使用道具 举报

发表于 2016-12-21 11:03 | 显示全部楼层
都适用的。在循环里加个筛选条件即可,比如 if sh.name like "*班" then x = x & "," & sh.Name
回复

使用道具 举报

 楼主| 发表于 2016-12-21 11:24 | 显示全部楼层
grf1973 发表于 2016-12-21 11:03
都适用的。在循环里加个筛选条件即可,比如 if sh.name like "*班" then x = x & "," & sh.Name

grf1973老师:应该怎样加这一句语句,我加了不能正常运行,显示无if 块,多谢指教。
回复

使用道具 举报

发表于 2016-12-21 11:31 | 显示全部楼层
  1. Sub 选择所有工作表()
  2.     Dim sh As Worksheet
  3.     For Each sh In Worksheets
  4.          If sh.Name Like "*班" Then x = x & "," & sh.Name
  5.     Next
  6.     x = Mid(x, 2)
  7.     If InStr(x, ",") > 0 Then Sheets(Split(x, ",")).Select Else If Len(x) > 0 Then Sheets(x).Select
  8. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-12-21 11:39 | 显示全部楼层

grf1973老师:如果选定工作表后进行页面设置(选定A4纸、居中、页边距等设置),应该加在哪里,用什么语句,多谢指教。
回复

使用道具 举报

发表于 2016-12-21 13:02 | 显示全部楼层
自己录个宏,改下。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 14:08 , Processed in 0.235368 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表