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
Bookmarks