Share via

Conditional Header

Anonymous
2012-08-29T00:14:35+00:00

Is it possible to have a conditional header based on the value in cell B2? For example, if the value in cell B2 is:

02  then Carroll & Associates to appear in the header,

03  then Hydraulic Components,

05  then Casting Sales, etc.

Please share your ideas.

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

Anonymous
2012-09-03T17:54:02+00:00

You could do either:

Public Sub Workbook_BeforePrint(Cancel As Boolean)

' This will load the header for each sheet according to the value of A2 on each sheet.'    Dim i As Long

    For i = 1 To ThisWorkbook.Worksheets.Count

        With ThisWorkbook.Worksheets(i).PageSetup

            Select Case LCase(ThisWorkbook.Worksheets(i).Range("A2").Text)

                Case "02"

                    .LeftHeader = "H. G. Carroll & Associates"

                Case "03"

                    .LeftHeader = "International Hydraulic Components"

                Case "05"

                    .LeftHeader = "Casting Sales"

                ' continue with other cases                Case Else

                    .LeftHeader = vbNullString

            End Select

        End With

    Next i

End Sub

or:

Public Sub Workbook_BeforePrint(Cancel As Boolean)

' This will load the header for each sheet according to the value of A2 on "Sheet1".'    Dim i As Long

    Dim strLHeader As String

    Select Case LCase(ThisWorkbook.Worksheets("Sheet1").Range("A2").Text)

        Case "02"

            strLHeader = "H. G. Carroll & Associates"

        Case "03"

            strLHeader = "International Hydraulic Components"

        Case "05"

            strLHeader = "Casting Sales"

      ' continue with other cases        Case Else

            strLHeader = vbNullString

    End Select

    For i = 1 To ThisWorkbook.Worksheets.Count

        ThisWorkbook.Worksheets(i).PageSetup.LeftHeader = strLHeader

    Next i

End Sub

Hope that helps.

Cheers

Rich

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2012-09-03T16:23:33+00:00

I was able to solve the "Run-time error '9' by expanding the code for each rep but  this does not appear to be an affective use of resources. Is there a more efficient way to code the following?

Public Sub Workbook_BeforePrint(Cancel As Boolean)

With ThisWorkbook.Worksheets("Rep - 02").PageSetup

    Select Case LCase(Range("A2").Text)

        Case "02"

            .LeftHeader = "H. G. Carroll & Associates"

        Case "03"

            .LeftHeader = "International Hydraulic Components"

        Case "05"

            .LeftHeader = "Casting Sales"

        ' continue with other cases

        Case Else

            .LeftHeader = vbNullString

    End Select

End With

With ThisWorkbook.Worksheets("Rep - 03").PageSetup

    Select Case LCase(Range("A2").Text)

        Case "02"

            .LeftHeader = "H. G. Carroll & Associates"

        Case "03"

            .LeftHeader = "International Hydraulic Components"

        Case "05"

            .LeftHeader = "Casting Sales"

        ' continue with other cases

        Case Else

            .LeftHeader = vbNullString

    End Select

End With

With ThisWorkbook.Worksheets("Rep - 05").PageSetup

    Select Case LCase(Range("A2").Text)

        Case "02"

            .LeftHeader = "H. G. Carroll & Associates"

        Case "03"

            .LeftHeader = "International Hydraulic Components"

        Case "05"

            .LeftHeader = "Casting Sales"

        ' continue with other cases

        Case Else

            .LeftHeader = vbNullString

    End Select

End With

End Sub

Was this answer helpful?

0 comments No comments

14 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-08-30T14:27:10+00:00

    I think he's asking about the Page Setup header not a "header" row on the worksheet. Code in the BeforeSave event would allow for a custom page header.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-08-30T11:35:48+00:00

    Here is the link:

    http://wikisend.com/download/122616/2310 v2.5 Commissions July 2012.xlsm

    Please ask if you have any questions.

    Thank you!

    Steve

    Was this answer helpful?

    0 comments No comments
  3. Ashish Mathur 101.9K Reputation points Volunteer Moderator
    2012-08-29T21:13:29+00:00

    Yes, you should.

    Please upload the file to some server and post the link to the uploaded file here.

    Was this answer helpful?

    0 comments No comments