Format to Headers in excel using VBA

bto.zapata

New Member
Joined
Jul 5, 2012
Messages
12
Hi there!

i have been trying to give special format to my workbook so that i can have a different header in each page of my excel so it can be ready to print. I've done my research and ended up by deciding to use VBA to do this. this is my code:
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
                 ws.PageSetup.LeftHeader = ws.Range("A2").Value
         Next ws
End Sub

this works great:cool:... but the value in "A2" allways appears in arial 12 with black colour:oops:... ¡even if i change it manualy previously!

Does anyone knows hoy to change the font type and font colour of a header using vba?

Thanks!;)
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I am not sure I get your request.

Is it the header's font you wan to change or range("A2")'s font.

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
                   With ws.Range("A2")
                      .Font.Color = 5
                      .Font.Name = "Calibri"
                      .Font.Size = 12
                  End With
       
                ws.PageSetup.LeftHeader = ws.Range("A2").Value
      Next ws
End Sub
 
Upvote 0
Another way.

In ThisWorkbook,

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim wks         As Worksheet

    For Each wks In ActiveWorkbook.Worksheets
        wks.PageSetup.LeftHeader = HdrFtr(ws.Range("A2").Value, "Calibri,Regular", vbBlue, 36)
    Next wks
End Sub

In a standard module,

Code:
Function HdrFtr(sText As String, _
                Optional ByVal sFont As String, _
                Optional iColor As Long, _
                Optional iFontSize As Long) As String
    ' shg 2012

    Dim sColor      As String
    Dim sFontSize   As String

    If Len(sFont) Then sFont = "&""" & sFont & """"

    If Abs(iFontSize) Then sFontSize = "&" & Abs(iFontSize)

    If iColor <> 0 Then
        sColor = "&K" & _
        Right("0" & Hex(iColor And &HFF), 2) & _
                 Right("0" & Hex(iColor \ &H100 And &HFF), 2) & _
                 Right("0" & Hex(iColor \ &H10000 And &HFF), 2)
    End If

    HdrFtr = sFont & sFontSize & sColor & sText
End Function
 
Upvote 0
Thanks for that! i'm almost there!

Creating a function in a module and add it to "This Workbook" was a great idea! :cool:

my intention was to give format to the headers, not to the cell range so tha was very helpful. now,

I'm sorry if i'm beeing a little anoying but... :confused:is there a way to give the HEADER a "fill colour" as you normaly do to a cell?:confused: It's becouse i'm trying to give it like a banner style but i need to have:

1)Left Header: different text on each page but to keep a brown background
2)Center Header: just the brown background
3)Right Header: my employees logo (allways)

I tried using an image that will cross all the worksheet if i place it in the right header my image covers the text in the left header so it won't be seen or printed.

¿Any ideas?
 
Upvote 0
is there a way to give the HEADER a "fill colour" as you normaly do to a cell?
Maybe using a picture; never tried.
 
Upvote 0
Never mind! problem solved... when tha macro runs again, the left header is applied and the image goes to the background leaving my "customable" header in front available to preview/print. thanks for the help thou
 
Upvote 0
Thou are welcome, good luck.
 
Upvote 0

Forum statistics

Threads
1,215,883
Messages
6,127,546
Members
449,385
Latest member
KMGLarson

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