将特定于每个页面的单元格值添加到每个标题

问题描述 投票:0回答:1

我试图让封面页标题显示“时间表”,然后让每个工作表的标题显示单元格“a6”中显示的日期,该日期位于每个工作表的单元格“a6”中。我一直在处理的代码将仅在所有工作表中显示 1 个工作表的单元格值(“时间表”,来自封面工作表)。我需要每张纸从它自己的单元格“a6”中提取并在标题中显示值。我怎样才能做到这一点?

Sub printless()
    Application.PrintCommunication = False
    Dim wkst As Worksheet
        For Each wkst In ActiveWorkbook.Sheets
            With wkst.PageSetup
                .CenterHeader = "&B&36" & Range("a6").Value ''''''BOLD/SIZE36, PULLS VALUE FROM CELL A6 AND PUT IS IN HEADER
                .PrintArea = "$a$6:$bb$108"
                .LeftMargin = Application.InchesToPoints(0.25)
                .RightMargin = Application.InchesToPoints(0.25)
                .TopMargin = Application.InchesToPoints(0.6)
                .BottomMargin = Application.InchesToPoints(0.25)
                .HeaderMargin = Application.InchesToPoints(0.3)
                .FooterMargin = Application.InchesToPoints(0.3)
                .CenterHorizontally = True
                .CenterVertically = False
                .Orientation = xlPortrait
                .PaperSize = xlPaperLetter
                .Zoom = 46
            End With
        Next wkst
            With Worksheets("Cover Sheet").PageSetup
                .CenterHeader = "&B&36" & Range("a6").Value
                .PrintArea = "$b$1:$ac$52"
                .LeftMargin = Application.InchesToPoints(0.25)
                .RightMargin = Application.InchesToPoints(0.25)
                .TopMargin = Application.InchesToPoints(1)
                .BottomMargin = Application.InchesToPoints(0.25)
                .HeaderMargin = Application.InchesToPoints(0.3)
                .FooterMargin = Application.InchesToPoints(0.3)
                .CenterHorizontally = True
                .CenterVertically = False
                .Orientation = xlPortrait
                .PaperSize = xlPaperLetter
                .Zoom = 75
            End With
    Application.PrintCommunication = True
    ActiveWorkbook.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
End Sub
excel vba
1个回答
0
投票

打印作业簿

  • 您的代码的关键问题已在您的评论中涵盖。
  • 您可以通过使用以下方法进行改进:
Sub printless()

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    ' If it isn't, reference the workbook by name or use 'ActiveWorkbook'.

    Application.PrintCommunication = False
    
    Dim wkst As Worksheet
    
    For Each wkst In wb.Worksheets
        With wkst.PageSetup
            Select Case wkst.Name
                Case "Cover Sheet"
                    .CenterHeader = "&B&36" & wkst.Range("A6").Value
                    .PrintArea = "$b$1:$ac$52"
                    .LeftMargin = Application.InchesToPoints(0.25)
                    .RightMargin = Application.InchesToPoints(0.25)
                    .TopMargin = Application.InchesToPoints(1)
                    .BottomMargin = Application.InchesToPoints(0.25)
                    .HeaderMargin = Application.InchesToPoints(0.3)
                    .FooterMargin = Application.InchesToPoints(0.3)
                    .CenterHorizontally = True
                    .CenterVertically = False
                    .Orientation = xlPortrait
                    .PaperSize = xlPaperLetter
                    .Zoom = 75
                Case Else
                    .CenterHeader = "&B&36" & wkst.Range("A6").Value
                    .PrintArea = "$a$6:$bb$108"
                    .LeftMargin = Application.InchesToPoints(0.25)
                    .RightMargin = Application.InchesToPoints(0.25)
                    .TopMargin = Application.InchesToPoints(0.6)
                    .BottomMargin = Application.InchesToPoints(0.25)
                    .HeaderMargin = Application.InchesToPoints(0.3)
                    .FooterMargin = Application.InchesToPoints(0.3)
                    .CenterHorizontally = True
                    .CenterVertically = False
                    .Orientation = xlPortrait
                    .PaperSize = xlPaperLetter
                    .Zoom = 46
            End Select
        End With
    Next wkst
    
    Application.PrintCommunication = True
    
    wb.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False

End Sub
  • 如果
    PageSetup
    与其余工作表之间的
    Cover Sheet
    参数的差异是最终的,您可以通过以下方式缩短代码:
Sub printless()

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    ' If it isn't, reference the workbook by name or use 'ActiveWorkbook'.

    Application.PrintCommunication = False
    
    Dim wkst As Worksheet
    
    For Each wkst In wb.Worksheets
        With wkst.PageSetup
            .CenterHeader = "&B&36" & wkst.Range("A6").Value
            .LeftMargin = Application.InchesToPoints(0.25)
            .RightMargin = Application.InchesToPoints(0.25)
            .BottomMargin = Application.InchesToPoints(0.25)
            .HeaderMargin = Application.InchesToPoints(0.3)
            .FooterMargin = Application.InchesToPoints(0.3)
            .CenterHorizontally = True
            .CenterVertically = False
            .Orientation = xlPortrait
            .PaperSize = xlPaperLetter
            Select Case wkst.Name
                Case "Cover Sheet"
                    .PrintArea = "$b$1:$ac$52"
                    .TopMargin = Application.InchesToPoints(1)
                    .Zoom = 75
                Case Else
                    .PrintArea = "$a$6:$bb$108"
                    .TopMargin = Application.InchesToPoints(0.6)
                    .Zoom = 46
            End Select
        End With
    Next wkst
    
    Application.PrintCommunication = True
    
    wb.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.