如何用vba设置excel页眉页脚

如何用vba设置excel页眉页脚,第1张

Sub test()

    Application.PrintCommunication = False

    With ActiveSheet.PageSetup

        .LeftHeader = ""            '页眉设置为:空

        .CenterHeader = "&F"        '页眉中设置为:文件名

        .RightHeader = ""           '页眉右设置为:空

        .LeftFooter = "&B 机密 &B"  '页脚左设置为:加粗的 机密两个字

        .CenterFooter = "&D"        '页脚中设置为:当前日期

        .RightFooter = " 第 &P 页"  '页脚右设置为:页码

    End With

    Application.PrintCommunication = True

End Sub

建议:学习vba,首先学会记录宏。

举例说明。

第一步:点击开发工具-录制宏。

第二步:设置页眉。如图:

第三步:停止录制。

刚才录制的代码如下:

Sub Macro1()

'

' Macro1 Macro

'

'

With ActiveSheet.PageSetup

.PrintTitleRows = ""

.PrintTitleColumns = ""

End With

ActiveSheet.PageSetup.PrintArea = ""

With ActiveSheet.PageSetup

.LeftHeader = ""

.CenterHeader = "&""-,加粗""&14测试"

.RightHeader = ""

.LeftFooter = ""

.CenterFooter = ""

.RightFooter = ""

.LeftMargin = Application.InchesToPoints(0.708661417322835)

.RightMargin = Application.InchesToPoints(0.708661417322835)

.TopMargin = Application.InchesToPoints(0.748031496062992)

.BottomMargin = Application.InchesToPoints(0.748031496062992)

.HeaderMargin = Application.InchesToPoints(0.31496062992126)

.FooterMargin = Application.InchesToPoints(0.31496062992126)

.PrintHeadings = False

.PrintGridlines = False

.PrintComments = xlPrintNoComments

.PrintQuality = 600

.CenterHorizontally = False

.CenterVertically = False

.Orientation = xlPortrait

.Draft = False

.PaperSize = xlPaperA4

.FirstPageNumber = xlAutomatic

.Order = xlDownThenOver

.BlackAndWhite = False

.Zoom = 100

.PrintErrors = xlPrintErrorsDisplayed

.OddAndEvenPagesHeaderFooter = False

.DifferentFirstPageHeaderFooter = False

.ScaleWithDocHeaderFooter = True

.AlignMarginsHeaderFooter = True

.EvenPage.LeftHeader.Text = ""

.EvenPage.CenterHeader.Text = ""

.EvenPage.RightHeader.Text = ""

.EvenPage.LeftFooter.Text = ""

.EvenPage.CenterFooter.Text = ""

.EvenPage.RightFooter.Text = ""

.FirstPage.LeftHeader.Text = ""

.FirstPage.CenterHeader.Text = ""

.FirstPage.RightHeader.Text = ""

.FirstPage.LeftFooter.Text = ""

.FirstPage.CenterFooter.Text = ""

.FirstPage.RightFooter.Text = ""

End With

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7745453.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存