Header Formatting in VBA

chipsworld

Board Regular
Joined
May 23, 2019
Messages
161
Office Version
  1. 365
I have a Sheet that I am trying to format the header on... I have the code to create the header, but can't get the line spacing right...

Any help would be great...

Here is what I have:

unitype and unitnam are strings from a userform.

When I run this is looks like this:

38 MP CO - DEMOB OUT

MASTER ROLL UP

07 May 2020

and I need it to look like this

38 MP CO - DEMOB OUT
MASTER ROLL UP
07 May 2020

How do I get rid of the line between each line??

VBA Code:
Sub addHeaders(unitnam)
Dim ws As Worksheet
Dim unitype As String

unitype = Me.cmbtype.Value

Set ws = Worksheets("Template")

Application.ScreenUpdating = False

     With ws.PageSetup
.CenterHeader = "&B""&12" & unitnam & " - " & unitype & vbCrLf & "MASTER ROLL UP" & vbCrLf & Format(Date, ("dd MMM yyyy"))
.CenterVertically = True
.CenterHorizontally = True
.AlignMarginsHeaderFooter = True
    End With

Set ws = Nothing
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
WOW...two in a row. I figured this one out...

Must use Chr(10) instead of vbCrLf for line change...

Thanks all!
 
Upvote 0
Solution
Must use Chr(10) instead of vbCrLf for line change...
That's right.

VBA Code:
vbCr = Chr(13) 'Carriage return
vbLf = Chr(10) ' Line feed
vbCrLf = Chr(13) & Chr(10) ' Combination of above ones

Glad you got the solution, and thanks for sharing for future readers.
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top