+ Reply to Thread
Results 1 to 9 of 9

Macro for Page Setup

Hybrid View

  1. #1
    Registered User
    Join Date
    01-14-2013
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    28

    Macro for Page Setup

    Hi,

    Please can someone help me.

    I have a spreadsheet used often which I want to be able to automate the Page / Print Setup as opposed to manually setting it up each time.

    Print area needs to start from A1
    Last column will always be H
    The last row will always fluctuate tho owing to data being greater or less than on the example

    The orientation will need to be Portrait

    Need this to be directed as an Active Sheet

    Help greatly appreciated.

    Thank you.
    Attached Files Attached Files

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,199

    Re: Macro for Page Setup

    I suggest you turn on the macro recorder, then apply these settings. Then provide the code and we can help to make the code apply to the ActiveSheet.

    To start printing in A1 through column H, simply set the print area to the entire columns A:H. It will automatically print to whatever is the last used row.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,719

    Re: Macro for Page Setup

    Or something like this.
    Sub Maybe_So()
    Dim lr As Long
    lr = ActiveSheet.Range("A:H").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With ActiveSheet.PageSetup
        .PrintArea = Range("A1:H" & lr).Address
        .Orientation = xlPortrait
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
        ActiveSheet.PrintOut
    End Sub
    The inherent weakness of the liberal society: a too rosy view of humanity.

  4. #4
    Registered User
    Join Date
    01-14-2013
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Macro for Page Setup

    This is perfect - thank you.

    Is there a way to add code which will generate a PDF file with the same file name and which is saved in the same folder location as the Excel file.

    I have looked at numerous code and cannot fathom it.

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,719

    Re: Macro for Page Setup

    Try so.
    Sub Maybe_So_2()
    Dim lr As Long, PDF As String
    lr = ActiveSheet.Range("A:H").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    PDF = Left(ThisWorkbook.FullName, (InStrRev(ThisWorkbook.FullName, ".", -1, vbTextCompare))) & "pdf"
    With ActiveSheet
            With .PageSetup
                .PrintArea = Range("A1:H" & lr).Address
                .Orientation = xlPortrait
                .Zoom = False
                .FitToPagesWide = 1
                .FitToPagesTall = 1
            End With
        .ExportAsFixedFormat xlTypePDF, PDF
        .PrintOut
    End With
    End Sub

  6. #6
    Registered User
    Join Date
    01-14-2013
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Macro for Page Setup

    Hi,

    That just prints the document. The PDF version wasn't created or saved as PDF.

    I tried the exportasfixedformat all day yesterday and nothing worked.

    It is probably a really simple piece of code but every website all seems to include something different.

    Any ideas?

    Thank you for your help.

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,719

    Re: Macro for Page Setup

    It works at this end here.
    It saves the PDF file in the same folder where the .xlsm workbook has been saved.
    If you can't find the problem, attach the file here. Change personal data like names, addresses email addresses etc if present before attaching.

  8. #8
    Registered User
    Join Date
    01-14-2013
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Macro for Page Setup

    It outputs as a PDF to where my Personal.xlsb is.

    C:\Users\xxxxx\AppData\Roaming\Microsoft\Excel\XLSTART

    I need the location of the newly generated PDF file to be exactly the same as the Excel file I have opened.

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,719

    Re: Macro for Page Setup

    Change "ThisWorkbook.FullName" to "ActiveWorkbook.FullName"

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VB Macro to do page setup
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-06-2014, 10:42 AM
  2. Page setup macro
    By jsmity in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2010, 10:48 AM
  3. Macro gets hung up on Page setup
    By sratkins in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-25-2009, 03:12 PM
  4. excel 4 macro - page.setup
    By rgarber1950 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2007, 10:33 AM
  5. [SOLVED] What does &L& mean in a page setup macro?
    By Bonnie June in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-26-2006, 06:35 PM
  6. Page Setup Macro
    By Darin Kramer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2005, 02:05 PM
  7. Page Setup by macro
    By Repeat3 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-29-2005, 08:37 AM

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