Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Printing range of sheets in excel or in PDF

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    14

    Printing range of sheets in excel or in PDF

    I have following macro which prints from the ranges defined, but if i want to print the same ranges and save them in one excel file as sheets separately. And also if i want to print the same and save as pdf file.

    Code:
    Sub Print_Ranges()
        Dim strShtname As String, strRngName As String
        Dim i As Long
            
        With 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
                
                'clear any existing print areas and reset to named ranges areas
                With Worksheets(strShtname)
                    .PageSetup.PrintArea = ""
                    .PageSetup.PrintArea = Range(strRngName).Address
                     .PrintOut
                  '  .PrintPreview
                End With
            Next i
        End With
        
    End Sub
    Kindly assist.
    ----
    excel_learner
    (Using XL 2007 & 2010)

    -------Learning never stops!

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

    Try this. untested.

    Code:
    Sub Print_Ranges()
    
        Dim strShtname As String, strRngName As String
        Dim i As Long, strFileName  As String
        
            
        With 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 = ThisWorkbook.Path & "\" & strShtname & Format(Date, "mm-dd-yy") & ".pdf"
                'clear any existing print areas and reset to named ranges areas
                With Worksheets(strShtname)
                    .PageSetup.PrintArea = ""
                    .PageSetup.PrintArea = Range(strRngName).Address
                    .PrintOut
                  '  .PrintPreview
                    
                    '// Save the print area as a PDF file
                    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, _
                        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                        IgnorePrintAreas:=False, OpenAfterPublish:=False
                End With
            Next i
        End With
        
    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)

  3. #3
    Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    14
    Thanks, I will test this..
    Can this code be changed to do this saving in excel file. in fact all the ranges are in named sheet and i want to extract them in same named sheet and save them as flat file without formulas (for circulation) and leaving other sheets where named ranges are not taken in 1 to 38.

    I would much appreciate that.

    Thanks once again.
    ----
    excel_learner
    (Using XL 2007 & 2010)

    -------Learning never stops!

  4. #4
    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)

  5. #5
    Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    14
    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!

  6. #6
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Try

    Code:
    .Range("A2").CurrentRegion.Sort key1:=.Range("A3"), order1:=xlAscending, Header:=xlYes, ordercustom:=1, Orientation:=xlTopToBottom
    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)

  7. #7
    Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    14
    Hi, Thank you, however, it loops back to this
    Code:
    strShtname = .Range(strRngName).Parent.Name
    and stops there. But it does copy first page in the range and opens new wb and paste it there, but copies with formulas and not with correct page setup.

    Please assist.
    ----
    excel_learner
    (Using XL 2007 & 2010)

    -------Learning never stops!

  8. #8
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Can you please attach the workbook ?
    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)

  9. #9
    Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    14
    Hi, Thanks, the file i have after cutting it short, is 1.4 mb, how do i attach it here as it allows only 102kb.
    ----
    excel_learner
    (Using XL 2007 & 2010)

    -------Learning never stops!

  10. #10
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Preserve the format and delete the data before uploading. Also delete unnecessary worksheets.
    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)

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
  •