Excel宏插入换行符

5
在我的研究中,我遇到了两种在VBA宏中插入回车符的可能性,使用Chr(10)或Chr(13)。我甚至看到过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

对我来说可以工作,你确定你的代码中没有限制标题大小以使第二行不显示吗? - Scott Craner
我会使用"&F" & vbCrLf & "&A" - iDevlop
我改成了 vbCrLf。现在回车符出现了,但选项卡名称没有出现。我不知道有什么特别限制标题大小的东西,但我会仔细检查一下。 - Mark
我什么都没看到,但如果有人想看一下,我会在这里发布代码。我不否认这个代码改动有效(我会检查答案),但它仍然没有完成整个行,这让我感到困惑。 - Mark
我想跟进一下这个问题。我发现如果我删除一些没有实际作用的额外代码行(它们只是在宏录制时存在),那么两种方法都可以工作。一旦我消除了像边距设置和空白页眉/页脚行之类的东西,回车和第二行就可以正常工作了。 - Mark
1个回答

7

我同意Patrick H.的观点,这段代码是可行的...

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接