Excel VBA
Excel VBA自动设置页边距、页眉页脚
2017-04-25 16:36:16

用VBA给当前Excel表自动设置页边距和页眉页脚。

通过代码自动控制报表的位置,同时自动添加工作表名作为报表页眉,加页数作为页脚。

详细代码:

Sub Excel设置页眉页脚()

'

    With ActiveSheet.PageSetup

        .LeftMargin = Application.InchesToPoints(0.7)

        .RightMargin = Application.InchesToPoints(0.6)

        .TopMargin = Application.InchesToPoints(0.5)

        .BottomMargin = Application.InchesToPoints(0.4)

        .HeaderMargin = Application.InchesToPoints(0.2)

        .FooterMargin = Application.InchesToPoints(0.2)

        .Zoom = False

        .FitToPagesWide = 1

        .FitToPagesTall = 1

    End With

    With ActiveSheet.PageSetup

        .LeftHeader = ""

        .CenterHeader = "&A"

        .RightHeader = ""

        .LeftFooter = ""

        .CenterFooter = ""

        .RightFooter = "&P/&N"

    End With

    ActiveWindow.SelectedSheets.PrintPreview

End Sub

效果图: