• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12841
  • Last Modified:

Set up dynamic print range in excel vba

I have a form where the data will vary depending on the physician selected.

I want to use VBA to control the printing of this form.  It works fine if I hard code the SET Print Range.  However I want to be able to expand and contract the Print Range.

The Row Titles from row 1 through 7.  The data lines start at Row 8 and go to Col R.

Here is my code to control the Set Print Range.

    With ActiveSheet.PageSetup
        .PrintTitleRows = "$1:$7"
        .PrintTitleColumns = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = "PrintArea"
    Application.PrintCommunication = False

I tried to set up a Named Range with the formula:

Offset('Physician Report'!$A$7,0,0,counta('Physician Report'!$A$8:$A$200), Counta('Physician Report1!$A$8:$R:$200))

This gave me just the headers.

Any suggestions?


1 Solution
Harry LeeCommented:

Try the following Code.

Sub PrintArea()

Dim WS As Worksheet, PntRng As Range, OffSetRw As Integer, OffSetClmn As Integer

Set WS = Sheets("Physician Report")

OffSetRw = WS.Cells(Rows.Count, 1).End(xlUp).Row
OffSetClmn = WS.Cells(1, Columns.Count).End(xlToLeft).Column

Range(Cells(7, 1), Cells(OffSetRw, OffSetClmn)).Select

    With ActiveSheet.PageSetup
        .PrintTitleRows = "$1:$7"
        .PrintTitleColumns = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = Selection.Address
    Application.PrintCommunication = False

End Sub

Open in new window

GPSPOWAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now