UDF to return page headers/footers text


April 7, 2008 – 4:07 pm by JP

Here’s a series of short UDFs that will return the page headers and footers for the active worksheet.

1
2
3
Function GetLeftHeader()
  GetLeftHeader = ActiveSheet.PageSetup.LeftHeader
End Function
1
2
3
Function GetCenterHeader()
  GetCenterHeader = ActiveSheet.PageSetup.CenterHeader
End Function
1
2
3
Function GetRightHeader()
  GetRightHeader = ActiveSheet.PageSetup.RightHeader
End Function
1
2
3
Function GetLeftFooter()
  GetLeftFooter = ActiveSheet.PageSetup.LeftFooter
End Function
1
2
3
Function GetCenterFooter()
  GetCenterFooter = ActiveSheet.PageSetup.CenterFooter
End Function
1
2
3
Function GetRightFooter()
  GetRightFooter = ActiveSheet.PageSetup.RightFooter
End Function

To use: Just put the function name in the cell, prefixed by an equal sign. For example, to get the left header text in cell A1:

=GetLeftHeader()

If you use any of these functions in a workbook other than the one where the code resides (ex: your PERSONAL.XLS workbook), you may have to prefix the code like this:

=PERSONAL.XLS!GetLeftHeader()

You can also use them in VBA code to get the associated property and take appropriate action, for example:

1
2
3
4
5
6
7
8
9
10
11
12
Sub TestMe()
Dim strMyHeader As String
 
strMyHeader = GetLeftHeader
 
If strMyHeader <> "My Company Name" Then
  ActiveSheet.PageSetup.LeftHeader = "Company Name"
Else
  MsgBox strMyHeader & " is your company's name."
End If
 
End Sub

HTH,
JP



Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Excel, UDF, VBA
Tags: , , , ,

This post has 68 views since April 7, 2008 – 4:07 pm.

Post a Comment

Browse Posts:


« Rows to Repeat at Top | Updated blog and new pages »