Results 1 to 9 of 9

Dynamic Print Range for PDF Export

Threaded View

  1. #1
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2007
    Posts
    113

    Dynamic Print Range for PDF Export

    Hi,

    I've got a workbook that runs through a series of filters, generates new sheets, and prints their contents to PDF files.

    I'm currently hard-coding a print range, "A1:J40", for the exported PDF files.
    However, what I would really like to be able to do is to have the range dynamically change based on the last row of available data.

    The reason for setting it to end at J40 is because that's the largest dataset I have encountered thus far, but that may likely change.

    I've attached a sample of my code below that generates the PDfs (with range selection in bold) as well as the actual workbook that I'm using to generate all of these PDFs.

    I would genuinely appreciate any help offered.
    Thanks in advance!


    Sub Dis()
    
        Dim rCell As Range, ws As Worksheet
        Dim myFile As String
        
    
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
    
        'Starts with sheet named 1010version
        'creates a temp sheet that has data filtered from 1010version sheet (range C or field 3 for District) into the temp sheet
        'copies that data out to new sheet based on Q1 name,
        'saves that sheet as PDF file to destination specified
        'deletes temp sheet and removes filter
        'rinse/repeat the steps above until it runs out of rcells from Range C
        With Sheets("1010version")
            Sheets.Add().Name = "Temp"
            .Range("C1", .Range("C" & Rows.Count).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("Temp").Range("C1"), Unique:=True
            For Each rCell In Sheets("Temp").Range("C2", Sheets("Temp").Range("C" & Rows.Count).End(xlUp))
                .Range("C1").AutoFilter field:=3, Criteria1:=rCell
                Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count))
                ws.Name = rCell
                .AutoFilter.Range.Copy ws.Range("A5")
                Sheets("1010messaged").Range("A1:K7").Copy Destination:=ws.Range("A" & Rows.Count).End(xlUp).Offset(2, 0)
                Sheets("1010messaged").Range("A13:K13").Copy Destination:=ws.Range("A1")
                Sheets("1010messaged").Range("A14:K14").Copy Destination:=ws.Range("A2")
                Sheets("1010messaged").Range("A15:K15").Copy Destination:=ws.Range("A3")
                Sheets("1010messaged").Range("A16:K16").Copy Destination:=ws.Range("A4")
                ActiveSheet.Cells.Columns.AutoFit
                
    
      'Assign Test2 Folder location for PDF output files
      Range("Q1").Select
        ActiveCell.FormulaR1C1 = _
            "=""C:\Users\hashari\Desktop\Test2\Dis""&R1C8&""_Week ""&WEEKNUM(NOW())-6&""_storeopsreporting""&""_2014_Store Productivity Report""&"".pdf"""
    
    
           With ActiveSheet.PageSetup 'margins and page settings for PDF file in next step, i.e. ws.Range(...)
                .Orientation = xlLandscape
                .FitToPagesWide = 1
                .FitToPagesTall = 1
                .Zoom = False 'this is what forces it to print to a single page
                .LeftMargin = Application.InchesToPoints(1.3)
                .RightMargin = Application.InchesToPoints(1)
                .TopMargin = Application.InchesToPoints(0.5)
                .BottomMargin = Application.InchesToPoints(1)
                .PrintArea = Selection.Address
           End With
                
    
    
        myFile = Range("Q1").Value
        
        
        'for up to 40 rows in a printout including headers, store data, and footer conditions
        ws.Range("A1:J40").ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=myFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
    
              
            Next rCell
            Sheets("Temp").Delete
            .AutoFilterMode = False
        End With
    
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. how to print using combobox and make dynamic range of a print area
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-04-2014, 07:26 AM
  2. how to macros print / export to word doc with dynamic data count
    By mastakillah in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-02-2013, 08:53 PM
  3. [SOLVED] Need ‘Set Print Area’ Code for Dynamic Print Range based on Conditions
    By dosbirn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2013, 12:13 PM
  4. [SOLVED] Automatically Print Once Dynamic Print Range is Identified
    By ccowman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-27-2013, 10:34 AM
  5. Print Titles with Dynamic Print Range
    By Daeghen in forum Excel General
    Replies: 0
    Last Post: 08-01-2011, 12:11 PM

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