cycle through data validation list to print pdf with a file name

Carla carla

New Member
Joined
Oct 29, 2022
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hello, at the moment I have a an excel report for 500 kids. The execl report is then populated to the pdf.
I have created a data validation list using the ID to string name, marks and other details.
When I print to pdf the file name appears to be the student ID.
Is there a way for me to set a file name for each file example the student name instead of ID?
 
Try:
VBA Code:
Sub Do_It()
    Application.ScreenUpdating = False
    Dim pdf_path As String, srcWS As Worksheet, rng As Range
    Set srcWS = Sheets("Sheet2")
    pdf_path = ThisWorkbook.Path & Application.PathSeparator
    With srcWS
        For Each rng In .Range("A3", .Range("A" & Rows.Count).End(xlUp))
            Range("J8") = rng
            Range("D8") = rng.Offset(, 1)
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=pdf_path & Range("D8") & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Next rng
    End With
    Application.ScreenUpdating = True
    MsgBox "Done", vbInformation, "All"
End Sub
 
Upvote 0

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).
Or, as I said in your original thread, if you have a VLOOKUP formula which looks up the data validation value (ID in J8) in a table of IDs and names and returns the name (in cell D8) related to the ID, then it's better for the user and the macro can also read the D8 cell values to use as the PDF file name.

Add this formula to D8:
Excel Formula:
=VLOOKUP(J8,Sheet2!A3:B13,2,FALSE)

This macro loops through the data validation values in J8 and saves the PDFs with the name in D8.

VBA Code:
Public Sub Create_PDFs()

    Dim destinationFolder As String
    Dim dataValidationCell As Range, dataValidationListSource As Range, dvValueCell As Range
   
    destinationFolder = ThisWorkbook.Path & "\"     'Same folder as workbook containing this macro
    '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("MATHS").Range("J8")
    
    'Source of data validation list
   
    Set dataValidationListSource = Evaluate(dataValidationCell.Validation.Formula1)
    
    'Create PDF for each data validation value, using cell D8 as the file name
   
    For Each dvValueCell In dataValidationListSource
        dataValidationCell.Value = dvValueCell.Value
        With Worksheets("MATHS")
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & .Range("D8").Value & ".pdf", _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        End With
    Next
       
End Sub
 
Upvote 0
Or, as I said in your original thread, if you have a VLOOKUP formula which looks up the data validation value (ID in J8) in a table of IDs and names and returns the name (in cell D8) related to the ID, then it's better for the user and the macro can also read the D8 cell values to use as the PDF file name.

Add this formula to D8:
Excel Formula:
=VLOOKUP(J8,Sheet2!A3:B13,2,FALSE)

This macro loops through the data validation values in J8 and saves the PDFs with the name in D8.

VBA Code:
Public Sub Create_PDFs()

    Dim destinationFolder As String
    Dim dataValidationCell As Range, dataValidationListSource As Range, dvValueCell As Range
  
    destinationFolder = ThisWorkbook.Path & "\"     'Same folder as workbook containing this macro
    '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("MATHS").Range("J8")
   
    'Source of data validation list
  
    Set dataValidationListSource = Evaluate(dataValidationCell.Validation.Formula1)
   
    'Create PDF for each data validation value, using cell D8 as the file name
  
    For Each dvValueCell In dataValidationListSource
        dataValidationCell.Value = dvValueCell.Value
        With Worksheets("MATHS")
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & .Range("D8").Value & ".pdf", _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        End With
    Next
      
End Sub
This works super perfect! Thank you so much
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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