Print function selecting wrong Type of Change option excel vba

OP says:

When I select “Return from leave” under the drop down “Type of Change”, my print macro prints as a “salary” change type, not “return from leave”

Assuming that the

“salary” change type

corresponds to the “default print” i.e.:

ws.PageSetup.PrintArea = ActiveWorkbook.Worksheets("Form Lists").Range("i2")

It seems that the reason the code provided always prints the default range, it’s because the lines that determine the printed output are validating the ActiveSheet.Name instead of the value in the “Type of Change field and print”

Solution proposed:
Change these lines to reflect the cell where the “Type of Change field and print” is located:

Replace ActiveSheet.Name with the corresponding cell.address i.e.: F10 and update as required the comparisons against “v1.20” and “v1.21”

If Right(ActiveSheet.Name, 5) = "v1.20" _
    Or Right(ActiveSheet.Name, 5) = "v1.21" _
        Or str = "Corporate" Then
            ws.PageSetup.PrintArea = ActiveWorkbook.Worksheets("Form Lists").Range("H2")

Else
    ws.PageSetup.PrintArea = ActiveWorkbook.Worksheets("Form Lists").Range("i2")

End If

It should become (comparison values shown as a reference, they should be updated in line with the choices in the drop-down list) :

If ActiveSheet.Range("F10").Value2 = "Return from leave" _
    Or str = "Corporate" Then
        ws.PageSetup.PrintArea = ActiveWorkbook.Worksheets("Form Lists").Range("H2")

Else
    ws.PageSetup.PrintArea = ActiveWorkbook.Worksheets("Form Lists").Range("i2")

End If

Note:
1. Avoid using ActiveWorkbook and ActiveSheet, suggest to replace all instances of them by: ThisWorkbook and datasheet respectively.
2. Additionally, I would suggest to review and incorporate the use of With statement and Select Case statement throughout your procedure.

Leave a Comment