Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel macro to insert carriage return

In my research, I've encountered two possibilities for inserting a carriage return into a header in a VBA macro, using Chr(10) or Chr(13). I've even seen code posted by Allen Wyatt at excel.tips.com that seems to do exactly what I'm attempting, but where he asserts it works, I have yet to see success.

Here is the basic code I am attempting to execute:

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

There is other formatting I'm doing, but it all succeeds. This line only produces the filename in the header ("&F"), but no return and no tab name on the second line. It also doesn't fail; it just continues right through this line.

This macro was originally recorded by me in Excel 2010, which I then augmented with additional automation for page formatting. I'm still running it under Excel 2010, and it has never worked correctly on this particular line. Does anyone have knowledge of what might be transpiring here?

Edit: Here is the full code from the original macro recording and my edits.

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
like image 512
Mark Avatar asked Sep 28 '17 19:09

Mark


1 Answers

Agree with Patrick H. This code works ...

Sub header()
With ActiveSheet.PageSetup
    .CenterHeader = "&F" & vbCrLf & "&A"
End With
End Sub
like image 57
XLmatters Avatar answered Sep 21 '22 15:09

XLmatters