Excel VBA Code to save each iteration of List in a separate PDF File

Angel_Code Help

New Member
Joined
Oct 17, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I'm extremely new to VBA coding.

My excel sheet has a data validation list in cell G4. Depending on the list value selected, the cells below use index match to get values from another sheet.

I need to figure out how to save a pdf version of this tab for every single list value.

This is the code I've pieced together so far from similar threads on this board, but this only saves a PDF file for the last option in the validation list instead of ALL the options. It's driving me crazy cause other people have claimed it works

Thanks for the help!


VBA Code:
Public Sub Create_PDFs()

Dim dataValidationCell As Range, dataValidationListSource As Range, dvValueCell As Range
Dim dropdownValue As String
Dim PDFfile As String, i As Long
Dim destinationFolder As String
destinationFolder = ThisWorkbook.Path
'destinationFolder = "C:\path\to\folder" 'Or specific folder
If Right(destinationFolder, 1) <> "" Then destinationFolder = destinationFolder & ""

'Cell containing data validation in-cell dropdown
Set dataValidationCell = Worksheets("Summary_Page").Range("g4")
'Source of data validation list
Set dataValidationListSource = Evaluate(dataValidationCell.Validation.Formula1)
'Create PDF for each data validation value

For Each dvValueCell In dataValidationListSource
dataValidationCell.Value = dvValueCell.Value

With dataValidationCell.Worksheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & dvValueCell.Value & ".PDF", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
Next
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,

this code works for me - please give it a try:

VBA Code:
Public Sub Create_PDFs1612314()
'https://www.mrexcel.com/board/threads/excel-vba-code-to-save-each-iteration-of-list-in-a-separate-pdf-file.1219556/

Dim rngDVCell           As Range      'cell with DV
Dim rngDVListSource     As Range      'list pf values for rngDVCell
Dim rngDV_Value         As Range      'each single value in rngDVListSource
Dim strPathFolder       As String     'drive and path to folder for save, should have a trailing \ for windows

strPathFolder = ThisWorkbook.Path
'strPathFolder = "C:\path\to\folder" 'Or specific folder
'///check for the trailing backslash and add it if necessary
If Right(strPathFolder, 1) <> Application.PathSeparator Then strPathFolder = strPathFolder & Application.PathSeparator

'Cell containing data validation in-cell dropdown
Set rngDVCell = Worksheets("Summary_Page").Range("G4")
'Source of data validation list
Set rngDVListSource = Evaluate(rngDVCell.Validation.Formula1)
'Create PDF for each data validation value

For Each rngDV_Value In rngDVListSource
  rngDVCell.Value = rngDV_Value.Value
  
  '/// specify the sheet and range to print
  With Worksheets("Summary_Page").Range("A1:G29")
      .ExportAsFixedFormat Type:=xlTypePDF, _
                            Filename:=strPathFolder & rngDV_Value.Value & ".PDF", _
                            Quality:=xlQualityStandard, _
                            IncludeDocProperties:=True, _
                            IgnorePrintAreas:=False, _
                            OpenAfterPublish:=False
  End With
Next rngDV_Value

Set rngDVListSource = Nothing
Set rngDVCell = Nothing

End Sub

If you plan to run the code multiple times you should alter the code and either add a number (or a date time stamp) or check if the output file already exists and delete it prior to exporting a new one.

Ciao,
Holger
 
Upvote 0
Thank you so much for your response. However, something is just off.. I tried to run this code on a validation list of 41 values and it's only exporting 13 of them. I'm not quite sure why or how the code is picking items from the list.
 
Upvote 0
Hi,

on my sample I used a list with 55 entries and could not find any problems (I added a date time stamp to the filename and ran the code twice), sample looks like

Cell Formulas
RangeFormula
A6:G14A6= "text " & $G$4
P1:P55P1="temp " & ROW()
Cells with Data Validation
CellAllowCriteria
G4List=myDVList


f you want to you can download the sample MrE_1612314 1219556 _Sample.xlsm

Ciao,
Holger
 
Upvote 0
Hi,

on my sample I used a list with 55 entries and could not find any problems (I added a date time stamp to the filename and ran the code twice), sample looks like

Cell Formulas
RangeFormula
A6:G14A6= "text " & $G$4
P1:P55P1="temp " & ROW()
Cells with Data Validation
CellAllowCriteria
G4List=myDVList


f you want to you can download the sample MrE_1612314 1219556 _Sample.xlsm

Ciao,
Holger
Hi Holger, I'd like to download the sample as well, this is exactly what I am in need of. Could you please re-upload it? Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top