Header Issue

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
Am using the following to attempt to use VBA to create my document header.

fName is a string variable that extracts the .xls/.xlsx/.xlsm from the filename. the pertinent portion of the code that I am using (with no success) is as follows

Code:
Activesheet.PageSetup.CenterHeader = "&C&B""Arial""&20" & fName
Help, Please.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try Recording a Macro, this will solve most sytax issues...

I tried using the record option, with selecting the file name option and it shows as...

Code:
ActiveSheet.PageSetup.CenterHeader = "&""Arial,Bold""&20&F"

...also, the ".xlsx" appears in the header.
 
Upvote 0
Try

Code:
Sub Foo()
Dim fName As String
With ActiveSheet
    fName = ThisWorkbook.Name
    pos = InStr(1, fName, ".", vbTextCompare) - 1
    fName = Left(fName, pos)
    .PageSetup.CenterHeader = "&""Arial,Bold""&20&fName"
End With
End Sub
 
Upvote 0
Try

Code:
Sub Foo()
Dim fName As String
With ActiveSheet
    fName = ThisWorkbook.Name
    pos = InStr(1, fName, ".", vbTextCompare) - 1
    fName = Left(fName, pos)
    .PageSetup.CenterHeader = "&""Arial,Bold""&20&fName"
End With
End Sub

That makes the full name appear with the .xlsx and "Name" on the end, too.
:banghead:
 
Upvote 0
You are Right, I just now went back to discover that; Sorry.. I'll work with some more to see if I can eliminate the "xls.Name" tacked on.. (???)
 
Upvote 0
I was able to locate this nugget....

Code:
 .CenterHeader = IIf(InStr(ThisWorkbook.Name, ".") = 0, ThisWorkbook.Name, Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1))
...so how do I then format the font?
 
Upvote 0
Ok, here you go...

Code:
Sub Foo()
Dim sName As String
With ActiveSheet
    sName = ThisWorkbook.Name
    pos = InStr(1, sName, ".", vbTextCompare) - 1
    sName = Left(sName, pos)
    .PageSetup.CenterHeader = "&""Arial,Bold""&20" & UCase(Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5))   'use 5 if ".xlsm" or use 4 if ".xls"
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,545
Messages
6,179,432
Members
452,915
Latest member
hannnahheileen

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