VB script to print drop down list to PDF

N1njaX55

New Member
Joined
Sep 4, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a spread sheet with a drop down list of users. I would like to create a VB script to be able to select each user, print to a PDF with file name and current date, and have it save to a particular location.
The drop down list is located in cell H2 and the file could be saved in c:\temp. I am brand new to VB and having a hard time figuring out where I'm going wrong. Any help would be appreciated. Thanks!!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Assuming the H2 dropdown is a data validation in-cell dropdown, try this macro:
VBA Code:
Public Sub Create_PDFs()

    Dim user As Range
    
    With ActiveSheet
        For Each user In Range(.Range("H2").Validation.Formula1)
            .Range("H2").Value = user.Value
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Temp\" & user.Value & Format(Date, " YYYYMMDD") & ".pdf", _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Next
    End With

End Sub
 
Upvote 0
I received the following error: Method 'Range' of object '_Worksheet' failed
 
Upvote 0
Is my assumption correct? What type of dropdown is it? If it's a data validation dropdown, please show the details of the Data Validation dialogue.
 
Upvote 0
Your assumption is correct, it is a data validation dropdown.
 

Attachments

  • Drop down.png
    Drop down.png
    25.4 KB · Views: 10
Upvote 0
Please post a screenshot of the Data Validation dialogue so that I can see the DV details. Like this:

1694113029538.png
 
Upvote 0
Here is the screen shot.
 

Attachments

  • data validation.png
    data validation.png
    10.2 KB · Views: 9
Upvote 0
I can't reproduce the error. I see that H2:I2 are merged cells and the DV List formula is a named range. With my test sheet set up like that the code works for me.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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