Share via

adding the username to a header

Anonymous
2011-02-09T11:54:22+00:00

Within 'Page Setup - Header/Footer - custom header', is it possible to add the username thus:

Printed on &[Date]

at &[Time]

by &[UserName]

where UserName is determined by the following function in a module:

Function UserName()

        UserName = Environ("UserName")

End Function 

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2011-02-09T12:06:55+00:00

You can't use &[UserName] - it won't be recognized. But you can use the BeforePrint event of the workbook (in the ThisWorkbook module) to set the header or footer using VBA. For example:

Private Sub Workbook_BeforePrint(Cancel As Boolean)

    Dim wsh As Worksheet

    For Each wsh In Me.Worksheets

        wsh.PageSetup.CenterFooter = "Printed on &D at &T by " & Environ("Username")

    Next wsh

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-02-09T13:06:59+00:00

    HansV,

    I have managed to get this to work by adding it to a control on the user form rather than using the BeforePrint event.

    Thanks.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-02-09T12:58:10+00:00

    HansV,

    Thanks for this. I do get an error message tho:

    "unable to set the CentreFooter property of the PageSetup class"

    I've tried just text rather than date, time and user just to see if that would work but I got the same message.

    Was this answer helpful?

    0 comments No comments