【问题标题】:Excel vba defined footerExcel vba定义的页脚
【发布时间】:2026-01-28 08:35:01
【问题描述】:

以下代码用于在将 Excel 工作表绘制为 pdf 时自动定义页眉/页脚。 唯一似乎不起作用的是 centerfooter 函数,它应该以 dd-mm-yyyy 格式给出“活动工作表名称 - 定义的日期(单元格工作表的值(“instellingen ").Cells(22, 2) ),这只会导致在中心页脚中以“mm-dd-yyyy”格式返回日期,忽略并且不返回活动表名称部分。

With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = "Company name"
        .RightHeader = ""
        .LeftFooter = Sheets("instellingen").Cells(20, 2).Value
        .CenterFooter = Activesheet.name & " - " & Sheets("instellingen").Cells(22, 2).Value
        .RightFooter = "Pagina &P van de &N"

谢谢:)

编辑。 (完整的宏)

Sub PlotPDF()
'
' PlotPDF Macro
'
' Sneltoets: Ctrl+Shift+P
'
    Application.PrintCommunication = False
    Application.Dialogs(xlDialogPrinterSetup).Show
    With ActiveSheet.PageSetup
        ActiveWindow.View = xlPageBreakPreview
    ActiveSheet.PageSetup.PrintArea = "$A:$N"
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = ""
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = "Company name"
        .RightHeader = ""
        .LeftFooter = Sheets("instellingen").Cells(20, 2).Value
        .CenterFooter = ActiveSheet.Name & " - " & Format(Sheets("instellingen").Cells(22, 2).Value, "dd-MM-yyyy")
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0.7)
        .RightMargin = Application.InchesToPoints(0.7)
        .TopMargin = Application.InchesToPoints(0.75)
        .BottomMargin = Application.InchesToPoints(0.75)
        .HeaderMargin = Application.InchesToPoints(0.3)
        .FooterMargin = Application.InchesToPoints(0.3)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlLandscape
        .Draft = False
        .PaperSize = xlPaperA4
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .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
    Application.PrintCommunication = True
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
End Sub

【问题讨论】:

  • 对我来说很好。您的代码中是否有任何可能导致跳过的错误处理?类似On Error Resume Next
  • 也适合我。我得到了活动表名称和时间,用斜线分隔。
  • 你能在这里放一个MsgBoxMsgBox (Activesheet.name & " - " & Sheets("instellingen").Cells(22, 2).Value)吗?
  • msgbox 可以工作,但是在绘制它时仍然只返回日期,我已将整个宏放在上面的问题中,也许我还缺少其他东西?
  • 如果我在两者之间输入一个输入,它确实有效ActiveSheet.Name & Chr(10) & Format(Sheets("instellingen").Cells(22, 2).Value, "dd-MM-yyyy") 不知道有什么区别:)

标签: vba excel date


【解决方案1】:

使用 Format() 函数格式化 .Cell(22, 2) 中的日期

.CenterFooter = ActiveSheet.Name & " - " & Format(Sheets("instellingen").Cells(22, 2).Value, "dd-MM-yyyy")

【讨论】:

  • 缺少的是 ActiveSheet.Name 而不是日期
最近更新 更多