UDF to return page headers/footers text

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

Function GetLeftHeader()
  GetLeftHeader = ActiveSheet.PageSetup.LeftHeader
End Function
Function GetCenterHeader()
  GetCenterHeader = ActiveSheet.PageSetup.CenterHeader
End Function
Function GetRightHeader()
  GetRightHeader = ActiveSheet.PageSetup.RightHeader
End Function
Function GetLeftFooter()
  GetLeftFooter = ActiveSheet.PageSetup.LeftFooter
End Function
Function GetCenterFooter()
  GetCenterFooter = ActiveSheet.PageSetup.CenterFooter
End Function
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:

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

Related Articles:

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button
Comments on this article are closed. Why?

Site last updated: February 12, 2012