Results 1 to 10 of 19

Thread: Printing range of sheets in excel or in PDF

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi,

    Again untested.

    Code:
    Sub Print_Ranges()
    
        Dim strShtname As String, strRngName As String
        Dim i As Long, strFileName  As String
        Dim wbkActive   As Workbook
        Dim wbkPDF      As Workbook
        Dim wbkNew      As Workbook
        Dim rngDest     As Range
        Dim RowsCount   As Long
        
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
        End With
        
        Set wbkActive = ThisWorkbook
        Set wbkPDF = Workbooks.Add
        Set rngDest = wbkPDF.Worksheets(1).Range("a1")
        
        With wbkActive.Worksheets("INDEX")
            
            'sort the named range list according to page number order
            .Range("A2").CurrentRegion.Sort key1:=Range("A3"), order1:=xlAscending, Header:=xlYes, ordercustom:=1, Orientation:=xlTopToBottom
        
            'loop through the cells and determine parent of named range and specific range addresses
            For i = 3 To 38
                strRngName = .Cells(i, 2).Text
                strShtname = Range(strRngName).Parent.Name
                
                strFileName = wbkActive.Path & "\" & strShtname & Format(Date, "mm-dd-yy")
                'clear any existing print areas and reset to named ranges areas
                With wbkActive.Worksheets(strShtname)
                    .PageSetup.PrintArea = ""
                    .PageSetup.PrintArea = Range(strRngName).Address
                  
                    '// Paste the data to the workbook for PDF
                    .Range(strRngName).Copy rngDest
                    RowsCount = .Range(strRngName).Rows.Count
                    Set rngDest = rngDest.Offset(RowsCount)
                    '// Paste the data to a new workbook
                    Set wbkNew = Workbooks.Add
                    .Range(strRngName).Copy wbkNew.Worksheets(1).Range("a1")
                    '// Save the print area as a new file
                    wbkNew.SaveAs strFileName, 51
                    wbkNew.Close
                    Set wbkNew = Nothing
                End With
            Next i
        End With
        
        wbkPDF.Worksheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                        wbkActive.Path & "\" & Format(Date, "mmmmyy") & ".pdf", _
                        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                        IgnorePrintAreas:=False, OpenAfterPublish:=False
        
        
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  2. #2
    Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    15
    Hi
    Sorry for getting back very late on this...however, i am getting run time error "1004" on this line

    .Range("A2").CurrentRegion.Sort key1:=Range("A3"), order1:=xlAscending, Header:=xlYes, ordercustom:=1, Orientation:=xlTopToBottom

    Kindly advise.
    ----
    excel_learner
    (Using XL 2007 & 2010)

    -------Learning never stops!

Similar Threads

  1. Replies: 3
    Last Post: 05-14-2013, 03:25 PM
  2. Replies: 2
    Last Post: 04-14-2013, 08:23 PM
  3. Print excel sheet to a PDF file
    By xander1981 in forum Excel Help
    Replies: 2
    Last Post: 12-14-2012, 02:16 PM
  4. Replies: 7
    Last Post: 08-08-2012, 10:24 AM
  5. Printing Sheets Based On Criteria VBA
    By excel_learner in forum Excel Help
    Replies: 1
    Last Post: 05-04-2011, 08:00 PM

Posting Permissions

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