Excel 插入回车的宏 return

Excel macro to insert carriage return

在我的研究中,我遇到了两种将回车 return 插入 VBA 宏中的 header 的可能性,使用 Chr(10) 或 Chr(13) .我什至看到 Allen Wyatt 在 excel.tips.com 上发布的代码似乎完全符合我的尝试,但他断言它有效的地方,我还没有看到成功。

这是我尝试执行的基本代码:

With ActiveSheet.PageSetup
    .CenterHeader = "&F" & Chr(10) & "&A"
End With

我正在做其他格式化,但都成功了。该行仅在 header ("&F") 中生成文件名,但在第二行没有 return 和选项卡名称。它也不会失败;它只是继续通过这条线。

这个宏最初是我在 Excel 2010 年录制的,然后我增加了页面格式化的额外自动化功能。我在 Excel 2010 年仍然 运行 它,并且它从未在这条特定的线路上正常工作。有谁知道这里可能发生的事情吗?

编辑:这是原始宏录制和我编辑的完整代码。

Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = ""
    .PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = ":"
    .PrintTitleColumns = ""
    .LeftHeader = ""
    .CenterHeader = "&F" & vbCrLf & "&A"
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = "Printed &D"
    .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 = True
    .PrintComments = xlPrintNoComments
    .PrintQuality = 600
    .CenterHorizontally = False
    .CenterVertically = False
    .Orientation = xlPortrait
    .Draft = False
    .PaperSize = xlPaperLetter
    .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
Application.PrintCommunication = True

同意 Patrick H 的观点。此代码有效...

Sub header()
With ActiveSheet.PageSetup
    .CenterHeader = "&F" & vbCrLf & "&A"
End With
End Sub