ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 6834|回复: 11

[原创] 关于EXCEL的智能排版设置(VBA打印设置)

[复制链接]

TA的精华主题

TA的得分主题

发表于 2013-1-13 22:07 | 显示全部楼层 |阅读模式

复制代码
把附件放置VBA加载项,右键会出现以上菜单。
做了几天,现在与大家分享
主要功能:选中区域,则针对选中区域排版,否则,针对整个工作薄排版。
自动计算,以最合适的设置排版。
AutoFit-Lite.rar (20.48 KB, 下载次数: 368)

未命名.jpg

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-1-13 22:08 | 显示全部楼层
本帖最后由 atday 于 2013-1-13 22:10 编辑
  1. Sub AutoPrint() '智能排版设置 BY 小李 注:选中区域,则针对选中区域排版,否则,针对整个工作薄排版
  2. Dim Dy, Col, i, ii As Double
  3. Dim Din As Variant
  4. 'On Error Resume Next '错误跳过
  5. Application.ScreenUpdating = False '关闭屏幕刷新

  6. If Selection.Count <> 1 Then '以下对工作表选定设置排版

  7. Dy = ActiveSheet.UsedRange.Count
  8. Din = Selection.Address 'SpecialCells(xlCellTypeVisible)
  9. ActiveSheet.PageSetup.PrintArea = Din 'ActiveSheet设置自动打印有数据的范围
  10. 'ActiveSheet.Range("A4:A1000").SpecialCells(xlCellTypeVisible).EntireRow.AutoFit '设置自动行高Rows(A1000).End(xlUp)
  11. Col = Range(Din).EntireColumn.Width 'Width:返回的是以磅为单位的值


  12. Select Case Col
  13.     Case Is < 600
  14.     With ActiveSheet.PageSetup '初始设置
  15.         .LeftMargin = Application.InchesToPoints(0.708661417322835) '左边距
  16.         .RightMargin = Application.InchesToPoints(0.708661417322835) '右边距
  17.         .TopMargin = Application.InchesToPoints(0.590551181102362) '上边距
  18.         .BottomMargin = Application.InchesToPoints(0.590551181102362) '下边距
  19.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  20.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  21.         .CenterHorizontally = True '水平居中
  22.         '.CenterVertically = False '垂直居中
  23.         .Orientation = xlPortrait '页面纵向
  24.         '.Orientation = xlLandscape '页面横向
  25.         .Draft = False
  26.         .PaperSize = xlPaperA4 'A4纸
  27.         .BlackAndWhite = False '是否黑白打印
  28.         .Zoom = False '是否缩放模式
  29.         .FitToPagesWide = 1
  30.         .FitToPagesTall = 200
  31.     End With
  32.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  33.     ActiveWindow.Zoom = 100 '窗口缩放大小
  34.     Case Is < 735
  35.     With ActiveSheet.PageSetup '初始设置
  36.         .LeftMargin = Application.InchesToPoints(0.393700787401575) '左边距
  37.         .RightMargin = Application.InchesToPoints(0.393700787401575) '右边距
  38.         .TopMargin = Application.InchesToPoints(0.393700787401575) '上边距
  39.         .BottomMargin = Application.InchesToPoints(0.393700787401575) '下边距
  40.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  41.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  42.         .CenterHorizontally = True '水平居中
  43.         '.CenterVertically = False '垂直居中
  44.         .Orientation = xlPortrait '页面纵向
  45.         '.Orientation = xlLandscape '页面横向
  46.         .Draft = False
  47.         .PaperSize = xlPaperA4 'A4纸
  48.         .BlackAndWhite = False '是否黑白打印
  49.         .Zoom = False '是否缩放模式
  50.         .FitToPagesWide = 1
  51.         .FitToPagesTall = 200
  52.     End With
  53.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  54.     ActiveWindow.Zoom = 100 '窗口缩放大小
  55.    
  56.          Case Is < 900
  57.     With ActiveSheet.PageSetup '初始设置
  58.         .LeftMargin = Application.InchesToPoints(0.708661417322835) '左边距
  59.         .RightMargin = Application.InchesToPoints(0.708661417322835) '右边距
  60.         .TopMargin = Application.InchesToPoints(0.393700787401575) '上边距
  61.         .BottomMargin = Application.InchesToPoints(0.393700787401575) '下边距
  62.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  63.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  64.         .CenterHorizontally = True '水平居中
  65.         '.CenterVertically = False '垂直居中
  66.         '.Orientation = xlPortrait '页面纵向
  67.         .Orientation = xlLandscape '页面横向
  68.         .Draft = False
  69.         .PaperSize = xlPaperA4 'A4纸
  70.         .BlackAndWhite = False '是否黑白打印
  71.         .Zoom = False '是否缩放模式
  72.         .FitToPagesWide = 1
  73.         .FitToPagesTall = 200
  74.     End With
  75.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  76.     ActiveWindow.Zoom = 100 '窗口缩放大小
  77.     Case Is < 1100
  78.     With ActiveSheet.PageSetup '初始设置
  79.         .LeftMargin = Application.InchesToPoints(0.393700787401575) '左边距
  80.         .RightMargin = Application.InchesToPoints(0.393700787401575) '右边距
  81.         .TopMargin = Application.InchesToPoints(0.393700787401575) '上边距
  82.         .BottomMargin = Application.InchesToPoints(0.393700787401575) '下边距
  83.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  84.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  85.         .CenterHorizontally = True '水平居中
  86.         '.CenterVertically = False '垂直居中
  87.         '.Orientation = xlPortrait '页面纵向
  88.         .Orientation = xlLandscape '页面横向
  89.         .Draft = False
  90.         .PaperSize = xlPaperA4 'A4纸
  91.         .BlackAndWhite = False '是否黑白打印
  92.         .Zoom = False '是否缩放模式
  93.         .FitToPagesWide = 1
  94.         .FitToPagesTall = 200
  95.     End With
  96.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  97.     ActiveWindow.Zoom = 100 '窗口缩放大小
  98.    
  99.     Case Is < 1400
  100.     With ActiveSheet.PageSetup '初始设置
  101.         .LeftMargin = Application.InchesToPoints(0.196850393700787) '左边距
  102.         .RightMargin = Application.InchesToPoints(0.196850393700787) '右边距
  103.         .TopMargin = Application.InchesToPoints(0.393700787401575) '上边距
  104.         .BottomMargin = Application.InchesToPoints(0.393700787401575) '下边距
  105.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  106.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  107.         .CenterHorizontally = True '水平居中
  108.         '.CenterVertically = False '垂直居中
  109.         '.Orientation = xlPortrait '页面纵向
  110.         .Orientation = xlLandscape '页面横向
  111.         .Draft = False
  112.         .PaperSize = xlPaperA4 'A4纸
  113.         .BlackAndWhite = False '是否黑白打印
  114.         .Zoom = False '是否缩放模式
  115.         .FitToPagesWide = 1
  116.         .FitToPagesTall = 200
  117.     End With
  118.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  119.     ActiveWindow.Zoom = 100 '窗口缩放大小
  120.     Case Is >= 1400
  121.     MsgBox "页面太大,超出自动设置范围!"

  122. End Select
  123.   

  124. Else '以下整个工作薄批量设置

  125. ii = Worksheets.Count
  126. For i = 1 To ii
  127. Dy = Worksheets(i).UsedRange.Count
  128. If Dy > 3 Then
  129. Din = Worksheets(i).UsedRange.Address
  130. Worksheets(i).PageSetup.PrintArea = Din 'ActiveSheet设置自动打印有数据的范围
  131. Worksheets(i).Activate
  132. 'Worksheets(i).Range("A4:A1000").SpecialCells(xlCellTypeVisible).EntireRow.AutoFit '设置自动行高Rows(A1000).End(xlUp)
  133. Col = Range(Din).EntireColumn.Width 'Width:返回的是以磅为单位的值


  134. Select Case Col
  135.     Case Is < 600
  136.     With Worksheets(i).PageSetup '初始设置
  137.         .LeftMargin = Application.InchesToPoints(0.708661417322835) '左边距
  138.         .RightMargin = Application.InchesToPoints(0.708661417322835) '右边距
  139.         .TopMargin = Application.InchesToPoints(0.590551181102362) '上边距
  140.         .BottomMargin = Application.InchesToPoints(0.590551181102362) '下边距
  141.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  142.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  143.         .CenterHorizontally = True '水平居中
  144.         '.CenterVertically = False '垂直居中
  145.         .Orientation = xlPortrait '页面纵向
  146.         '.Orientation = xlLandscape '页面横向
  147.         .Draft = False
  148.         .PaperSize = xlPaperA4 'A4纸
  149.         .BlackAndWhite = False '是否黑白打印
  150.         .Zoom = False '是否缩放模式
  151.         .FitToPagesWide = 1
  152.         .FitToPagesTall = 200
  153.     End With
  154.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  155.     ActiveWindow.Zoom = 100 '窗口缩放大小
  156.    
  157.     Case Is < 735
  158.     With Worksheets(i).PageSetup '初始设置
  159.         .LeftMargin = Application.InchesToPoints(0.393700787401575) '左边距
  160.         .RightMargin = Application.InchesToPoints(0.393700787401575) '右边距
  161.         .TopMargin = Application.InchesToPoints(0.393700787401575) '上边距
  162.         .BottomMargin = Application.InchesToPoints(0.393700787401575) '下边距
  163.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  164.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  165.         .CenterHorizontally = True '水平居中
  166.         '.CenterVertically = False '垂直居中
  167.         .Orientation = xlPortrait '页面纵向
  168.         '.Orientation = xlLandscape '页面横向
  169.         .Draft = False
  170.         .PaperSize = xlPaperA4 'A4纸
  171.         .BlackAndWhite = False '是否黑白打印
  172.         .Zoom = False '是否缩放模式
  173.         .FitToPagesWide = 1
  174.         .FitToPagesTall = 200
  175.     End With
  176.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  177.     ActiveWindow.Zoom = 100 '窗口缩放大小
  178.    
  179.          Case Is < 900
  180.     With Worksheets(i).PageSetup '初始设置
  181.         .LeftMargin = Application.InchesToPoints(0.708661417322835) '左边距
  182.         .RightMargin = Application.InchesToPoints(0.708661417322835) '右边距
  183.         .TopMargin = Application.InchesToPoints(0.393700787401575) '上边距
  184.         .BottomMargin = Application.InchesToPoints(0.393700787401575) '下边距
  185.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  186.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  187.         .CenterHorizontally = True '水平居中
  188.         '.CenterVertically = False '垂直居中
  189.         '.Orientation = xlPortrait '页面纵向
  190.         .Orientation = xlLandscape '页面横向
  191.         .Draft = False
  192.         .PaperSize = xlPaperA4 'A4纸
  193.         .BlackAndWhite = False '是否黑白打印
  194.         .Zoom = False '是否缩放模式
  195.         .FitToPagesWide = 1
  196.         .FitToPagesTall = 200
  197.     End With
  198.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  199.     ActiveWindow.Zoom = 100 '窗口缩放大小

  200.     Case Is < 1100
  201.     With Worksheets(i).PageSetup '初始设置
  202.         .LeftMargin = Application.InchesToPoints(0.393700787401575) '左边距
  203.         .RightMargin = Application.InchesToPoints(0.393700787401575) '右边距
  204.         .TopMargin = Application.InchesToPoints(0.393700787401575) '上边距
  205.         .BottomMargin = Application.InchesToPoints(0.393700787401575) '下边距
  206.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  207.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  208.         .CenterHorizontally = True '水平居中
  209.         '.CenterVertically = False '垂直居中
  210.         '.Orientation = xlPortrait '页面纵向
  211.         .Orientation = xlLandscape '页面横向
  212.         .Draft = False
  213.         .PaperSize = xlPaperA4 'A4纸
  214.         .BlackAndWhite = False '是否黑白打印
  215.         .Zoom = False '是否缩放模式
  216.         .FitToPagesWide = 1
  217.         .FitToPagesTall = 200
  218.     End With
  219.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  220.     ActiveWindow.Zoom = 100 '窗口缩放大小
  221.     Case Is < 1400
  222.     With Worksheets(i).PageSetup '初始设置
  223.         .LeftMargin = Application.InchesToPoints(0.196850393700787) '左边距
  224.         .RightMargin = Application.InchesToPoints(0.196850393700787) '右边距
  225.         .TopMargin = Application.InchesToPoints(0.393700787401575) '上边距
  226.         .BottomMargin = Application.InchesToPoints(0.393700787401575) '下边距
  227.         .HeaderMargin = Application.InchesToPoints(0.31496062992126) '页眉边距
  228.         .FooterMargin = Application.InchesToPoints(0.31496062992126) '页脚边距
  229.         .CenterHorizontally = True '水平居中
  230.         '.CenterVertically = False '垂直居中
  231.         '.Orientation = xlPortrait '页面纵向
  232.         .Orientation = xlLandscape '页面横向
  233.         .Draft = False
  234.         .PaperSize = xlPaperA4 'A4纸
  235.         .BlackAndWhite = False '是否黑白打印
  236.         .Zoom = False '是否缩放模式
  237.         .FitToPagesWide = 1
  238.         .FitToPagesTall = 200
  239.     End With
  240.     ActiveWindow.View = xlPageBreakPreview '打印模式窗口
  241.     ActiveWindow.Zoom = 100 '窗口缩放大小
  242.     Case Is >= 1400
  243.     MsgBox "页面太大,超出自动设置范围!"

  244. End Select
  245. End If
  246. Next i
  247. End If
  248. Application.ScreenUpdating = True '开启屏幕刷新
  249. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2013-1-14 10:11 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2013-1-14 21:19 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2013-1-17 13:05 | 显示全部楼层
要是能添加 设置可以修改纸张类型就好了~~~~

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-1-26 22:55 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
xuyeshun1226 发表于 2013-1-17 13:05
要是能添加 设置可以修改纸张类型就好了~~~~

不是有吗?

TA的精华主题

TA的得分主题

发表于 2013-3-24 10:46 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
些分享,这个不错

TA的精华主题

TA的得分主题

发表于 2013-3-19 12:43 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2013-8-1 21:30 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2013-10-17 14:50 | 显示全部楼层
找了好久终于找到了,楼主好强大,谢谢分享
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-2 07:42 , Processed in 0.041789 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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