Excel宏以插入回车符

时间:2017-09-28 19:36:42

标签: excel vba excel-vba carriage-return

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

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

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

我还有其他格式,但一切都成功了。此行仅在标题中生成文件名("& F"),但第二行没有返回且没有标签名称。它也不会失败;它只是继续通过这条线。

这个宏最初由我在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 = "$1:$1"
    .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

1 个答案:

答案 0 :(得分:5)

同意Patrick H.此代码有效......

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