Set file name in Header without extension

Sweeti

New Member
Joined
May 8, 2021
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
Hello everybody,

I have this code in VBA:
Ws.PageSetup.RightHeader = "&F" & vbCr & "Sweeti"

This code work, but I need to have the file name without the extension.
Also I wish to set the font/size/color for the first line and I don't know how to insert in the above code without errors.

Can you help?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi,​
try Ws.PageSetup.RightHeader = Left$(Name, InStrRev(Ws.Parent.Name, ".") & vbLf & "Sweeti"
 
Upvote 0
Hi,​
try Ws.PageSetup.RightHeader = Left$(Name, InStrRev(Ws.Parent.Name, ".") & vbLf & "Sweeti"
Hi,
I got "Syntax Error" :( .

This is my full code:

Sub Add_Header_Footer()
Dim sPath As String
Dim Wb As Workbook, Ws As Worksheet
Dim sFile As String
sPath = "C:\Users\Sweeti\Desktop\New Documents\"
sFile = Dir(sPath & "*.xlsx")
Application.ScreenUpdating = False
Do While sFile <> ""
Set Wb = Workbooks.Open(sPath & sFile)
For Each Ws In Wb.Worksheets
Ws.PageSetup.RightHeader = "&F" & vbCr & "Sweeti"
Ws.PageSetup.RightFooter = "This Document" & vbCr & "Approved by:"
Next Ws
Wb.Close SaveChanges:=True
sFile = Dir
Loop
Application.ScreenUpdating = True
End Sub

With this code I get everything the way I want except for the unnecessary extension..
 
Upvote 0
Try Ws.Parent.Name rather than Name alone …​
 
Upvote 0
In the same time I have edited my previous post so to read & apply …​
 
Upvote 0
In the same time I have edited my previous post so to read & apply …​
I got Compile Error:
Expected: list separator or)

I replaced the line in this:
Ws.PageSetup.RightHeader = Left$(Ws.Parent.Name, InStrRev(Ws.Parent.Name, ".") & vbLf & "Sweeti"

Am I do something wrong?
 
Upvote 0
My bad again but obvious according to the error message :​
Rich (BB code):
Ws.PageSetup.RightHeader = Left$(Ws.Parent.Name, InStrRev(Ws.Parent.Name, ".") - 1) & vbLf & "Sweeti"
 
Upvote 0
Solution
My bad again but obvious according to the error message :​
Rich (BB code):
Ws.PageSetup.RightHeader = Left$(Ws.Parent.Name, InStrRev(Ws.Parent.Name, ".") - 1) & vbLf & "Sweeti"
Hi,
First of all it's work ! Thanks!

BUT (OMG) I did a mistake, the code suppose to be:
Ws.PageSetup.RightHeader = Left$(Ws.Parent.Name, InStrRev(Ws.Parent.Name, ".") - 1) & vbLf & "Page &[Page] of &[Pages]"

I have 2 problems:
1. It doesn't show the "Page of Pages".
2. The font size is 11 and I wish it to be 10 hence why I asked in first massage how to set the font/size/color.

Can you please ReEdit the code?
 
Upvote 0

An easy way : activate the Macro Recorder and set the header manually in order to get the generated code according to your will …​
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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