+ Reply to Thread
Results 1 to 5 of 5

Setting print area in a macro

  1. #1
    Registered User
    Join Date
    03-01-2012
    Location
    Orlando, Florida
    MS-Off Ver
    Excel 2007
    Posts
    2

    Setting print area in a macro

    I am new to writting macros, I have tried two different methods to set the range for the PrintArea in a macro, neither work, See below

    Sub CountRows()
    Dim RowCount As Integer
    Dim FinalCell As String
    Dim row1 As Integer
    Dim colstart As String
    Dim colend As String
    Dim RowsE As Double

    row1 = 8 'Beginning ROW number

    colstart = "$A$8" 'PrintArea Starting location

    colend = "$J$" 'PrintArea ending column

    'Calculate the number of rows in the spreadsheet

    Dim NumberOfRows As Long
    With Sheet1
    NumberOfRows = .Cells(.Rows.Count, 1).End(xlUp).Row
    NumberOfRows = .Range("A" & .Rows.Count).End(xlUp).Row
    MsgBox NumberOfRows

    End With

    FinalCell = colend & NumberOfRows 'PrintArea ending cell


    MsgBox FinalCell


    'Worksheets("Parts List").PageSetup.PrintArea = "$A$8:$J$26" ' THIS WORKS
    'Worksheets("Parts List").PageSetup.PrintArea = "$A$8:FinalCell"

    'Worksheets("Parts List").PageSetup.PrintArea = "Cells(8,1):Cells("FinalCell",10)"
    'Worksheets("Parts List").PageSetup.PrintArea = "Cells(8,1):Cells(26,10)" 'THIS WORKS

    End Sub


    What am I doing wrong?

    Thanks

  2. #2
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: Setting print area in a macro

    Does the code fail? If so at what line does the code fail...

    BTW You need to add code tags to Your posts or the Mods will chase You !!

  3. #3
    Registered User
    Join Date
    03-01-2012
    Location
    Orlando, Florida
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Setting print area in a macro

    If the line below is un commented then

    Worksheets("Parts List").PageSetup.PrintArea = "$A$8:FinalCell"

    I get "Run-time error '1004': The text you entered is not a valid reference or defin

    BTW what is a "code Tag"?

  4. #4
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: Setting print area in a macro

    You will as FinalCEll reports $j$1 not J = 10 and 10 is what you require

    so the code needs to read

    $A$8:$J:$1

    trouble is you have this $A$:$J$1 & (1) ' rem CRASH

    so you need this $A$:$J & 1

    Does that make sence?

  5. #5
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,275

    Re: Setting print area in a macro

    Quote Originally Posted by pkenard View Post
    Worksheets("Parts List").PageSetup.PrintArea = "$A$8:FinalCell"
    Try changing the above line to:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1