Forum Discussion

MrSassyBritches's avatar
MrSassyBritches
Copper Contributor
Feb 03, 2020

Cell value in header

PS: If there is another way to do this, like reference a named section somehow from within the header, that's what the ^[Doc] was meant to be. But everything online points to VBA.

I would the page header to display some values from the workbook as values in the header.  I did google this and none of the results we're able to produce anything working. Not even any errors, the headers just stay blank.
So here is what I would like to be referenced:

 

And here is the VBA code I have been trying to make work. This is just the latest snippet I have worked with, Its been a few days of scouring the internet for something that will work. I am convinced it's my implementation that is incorrect. 

 

Function Author()
Author = ThisWorkbook.BuiltinDocumentProperties("Author")
End Function

 

Function Doc()
Doc = Worksheets("WorkInstructions").Range("D2").Value = ThisWorkbook.CustomDocumentProperties.Item("Doc").Value
End Function

 

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim sTemp As String

With Worksheets("WorkInstructions")
sTemp = .Range("Z4").Text
PageSetup.LeftHeader = sTemp
End With
End Sub

Any input on this matter would be greatly appreciated, even if you don't think you have the answer. 🙂

18 Replies

  • Charla74's avatar
    Charla74
    Iron Contributor
    The VBA solution didn’t work?

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    ActiveSheet.PageSetup.CenterHeader = Range("Z4").Value
    End Sub
    • MrSassyBritches's avatar
      MrSassyBritches
      Copper Contributor
      So the code I found online, in my original post at the top, I could not get to work. The functions Author() and Doc() do work. I just tried what you posted and could not get it to work, do I need something like ^[Workbook_BeforePrint] in the header or something along those lines?
      • Charla74's avatar
        Charla74
        Iron Contributor
        You need to create the code for the workbook, not in a sheet.
        Open VBA editor, double click ‘This Workbook’ in the Microsoft Excel Objects folder (in the ‘Project - VBAProject’ panel to the left). In the code window, you will see two drop down menus; in the first select Workbook and the second Before Print...then type the rest of the code as below.
    • MrSassyBritches's avatar
      MrSassyBritches
      Copper Contributor
      The text. Whatever text is in the cell Z2 I want to be displayed in the top left of the header.

Resources