Cell value in header

Copper Contributor

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:

clipboard_image_1.png

 

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

@MrSassyBritchesPlease elaborate as to the determining factor for what data appears..?

The text. Whatever text is in the cell Z2 I want to be displayed in the top left of the header.
within the sheet or when printed ,, or both ??

Well the header wont be visible until printed, so when printed I guess.

I probably don't fully understand.

But can you not insert a formula at A1 that references Z2..?

@MrSassyBritches 

How bout making a1 ( or what ever cell you choose ), =z2 and then designate row 1 as a row title for printing.?

 

would it be better to discuss via phone..?

@MrSassyBritches 

 

think it would be best if we discussed via phone ?

I can assure you my communication skills only get worse verbally. I am not looking for a row title, as that is already present in my workbook.

Here is a screenshot to visualize.

Here it is now

clipboard_image_0.png

 

and here is what I am looking for (notice the 100 in the top left)

clipboard_image_1.png

 

I dont want the user to have to edit the header, they edit that cell next to "Doc" and have the header use that cell.

@MrSassyBritches 

thinkin I understand now.....  researching

The VBA solution didn’t work?

Private Sub Workbook_BeforePrint(Cancel As Boolean)
ActiveSheet.PageSetup.CenterHeader = Range("Z4").Value
End Sub
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?
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

Ok, so I just tested this myself and also couldn't get it to work. I found a solution (or rather a workaround) from the Microsoft support pages....So, if you do as per my last post BUT also enter the same code in a regular module, it will work. Although the background code is 'BeforePrint' the header does not actually display until it has been printed.

Let me know if you can get it working.

@Charla74 Thanks so much for the reply. I think I followed your instructions. Here is a screenshot of "ThisWorkbook" and the exact same code is inside "Module1". Does this look correct per your instructions?

clipboard_image_0.png

@MrSassyBritches

Looks good - Give her a print and let me know if it works.

@Charla74 I printed and got an error:

clipboard_image_2.png

As for the result, there was nothing printed on the center header :\
First I printed to a PDF, I then printed to a physical printer and both had the same result, no center header.

 

I wasn’t sure but I suspected that the two FUNCTION segments, in the Worksheet code, may cause an issue. If you remove those two parts of the code and leave them as they are in the regular module you may have a better outcome.