多语言展示
当前在线:1143今日阅读:26今日分享:39

Excel VBA 页面设置

对于经常做Excel表格的小伙伴们,经常需要设置Excel的打印页面和字体、页眉页脚等,那么如何使用VBA来完成重复的操作呢?我们一起学习一下。
工具/原料
1

电脑

2

office excel

方法/步骤
1

打开新建一个并打开EXCEL工作表

2

使用Alt+F11组合快捷键进入vbe编辑器或者单击【开发工具】-VIsual Basic,并插入一个新的模块。如图所示。

3

在模块中编写如下代码:Sub 初始化设置()    Cells.Select    Selection.RowHeight = 16       Selection.ColumnWidth = 9         With ActiveSheet.PageSetup             .RightFooter = ""       .LeftMargin = Application.InchesToPoints(0.0787)        .RightMargin = Application.InchesToPoints(0.0787)        .TopMargin = Application.InchesToPoints(0.592)        .BottomMargin = Application.InchesToPoints(0.3937)        .HeaderMargin = Application.InchesToPoints(0.0787)        .FooterMargin = Application.InchesToPoints(0.0787)        .Zoom = 100       End WithEnd Sub这部分代码单位英寸。

4

如果我们想直接设置厘米单位,我们这一将代码改为如下:Sub 初始化设置()    ActiveSheet.Cells.RowHeight = 16    ActiveSheet.Cells.ColumnWidth = 9  With ActiveSheet.PageSetup    .LeftMargin = Application.CentimetersToPoints(0.5)    .RightMargin = Application.CentimetersToPoints(0.5)    .TopMargin = Application.CentimetersToPoints(1.5)    .BottomMargin = Application.CentimetersToPoints(1) '底    .HeaderMargin = Application.CentimetersToPoints(0.5) '页眉    .FooterMargin = Application.CentimetersToPoints(0.5) '页脚    .Zoom = 100    End WithEnd Sub

5

其设置的效果为行高16,列宽9 ,页面设置如下图所示。

注意事项
1

本代码为个人经验,仅供参考,如有更好的方法,欢迎留言讨论。

2

本代码本人一直使用,没有BUG

推荐信息