+ Reply to Thread
Results 1 to 4 of 4

Macro to Auto-adjust print area in a multiple sheets (excel 2010)

Hybrid View

  1. #1
    Registered User
    Join Date
    01-11-2014
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    46

    Macro to Auto-adjust print area in a multiple sheets (excel 2010)

    I have a multiple sheets (tabs) where each sheet have variable print range. 1 sheet may need to print 1000 rows, others may need 10 rows only to print. The good thing is that, my column range is fixed at B:G. My criteria of including a row will be the last cell under column G with actual value. It's because all of these columns are populated with vlookup formulas. It should not inculde a row with a value of "" under column G.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Macro to Auto-adjust print area in a multiple sheets (excel 2010)

    Hi there,

    Try inserting the following code into the "ThisWorkbook" VBA CodeModule:


    
    
    
    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    
        Const sCOLUMN_TO_CHECK  As String = "G:G"
        Const sPRINT_AREA       As String = "$B$1:$G$"
    
        Dim rLastCell           As Range
        Dim iLastRow            As Integer
        Dim wks                 As Worksheet
    
        For Each wks In ThisWorkbook.Worksheets
    
            Set rLastCell = wks.Columns(sCOLUMN_TO_CHECK).Cells(1, 1)
    
            Do While rLastCell.Offset(1, 0).Value <> vbNullString
                Set rLastCell = rLastCell.Offset(1, 0)
            Loop
    
            iLastRow = rLastCell.Row
    
            wks.PageSetup.PrintArea = sPRINT_AREA & iLastRow
    
        Next wks
    
    End Sub

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    01-11-2014
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Macro to Auto-adjust print area in a multiple sheets (excel 2010)

    Hi,

    I pasted the above code to "This Workbook" but if I print preview, I'm still seeing hundreds of pages instead of only 1 as per the criteria. It means that nothing happens. I'm still hopeful that someone can help me. thanks.

  4. #4
    Registered User
    Join Date
    01-11-2014
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Macro to Auto-adjust print area in a multiple sheets (excel 2010)

    hi, can anyone help me?

+ 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. Auto Adjust Print Area
    By bgreen in forum Excel General
    Replies: 7
    Last Post: 01-02-2014, 01:42 AM
  2. [SOLVED] Set Print Area across multiple sheets
    By Krandamor in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-17-2012, 09:15 PM
  3. Replies: 0
    Last Post: 08-19-2009, 12:47 PM
  4. Adjust Print Area Macro
    By Traymond in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-13-2009, 03:16 PM
  5. Macro to Adjust Print Area
    By JustinL in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-16-2008, 09:30 PM

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