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?

thanks

Glen
GPSPOWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harry LeeCommented:
GPSPOW,

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
Range("A1").Select

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GPSPOWAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.