How to include date range in my PDF export name

druck21

New Member
Joined
Nov 15, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm following this youtube video to create a report that can be exported into PDFs with a macro. I want to add a date range to the filename and I can't figure out how to get it to work. I've put the date range in cell C6. Here is the macro I currently have, I've tried basically replicating the tempPDFFilePath replace part for the Date but it doesn't work so I've removed it here. This macro currently works perfectly it just doesn't give me the date in the filename. Any help would be appreciated!

VBA Code:
Sub SavePDFsFromList()

'Declare the Variables
Dim ws As Worksheet
Dim rngID As Range
Dim rngListStart As Range
Dim rowsCount As Long
Dim i As Long
Dim pdfFilePath As String
Dim tempPDFFilePath As String

'Stop the screen updating while running
Application.ScreenUpdating = False

'Reference Report Sheet
Set ws = ActiveWorkbook.Sheets("Report")

'Reference the Tech name cell
Set rngID = ws.Range("C5")

'Reference the start of the Tech name List
Set rngListStart = ws.Range("N6")

'Reference dates
Set dateID = ws.Range("C6")

'Create the PDF File Name
pdfFilePath = "C:\Users\name\Documents\Reports\Combined Tech Bonus and Pay Adj\PDFs\Tech Pay - [ID].pdf"

'Count the rows in the Tech name List
rowsCount = rngListStart.CurrentRegion.Rows.Count - 1

For i = 1 To rowsCount
    
    'Change the current tech name
    rngID.Value = rngListStart.Offset(i - 1, 0).Value
    
    'Replace [ID] with the Student ID Value
    tempPDFFilePath = Replace(pdfFilePath, "[ID]", rngID.Value)
    
    'Create the PDF
    ws.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=tempPDFFilePath

Next i

'Restart the screen updating
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
tempPDFFilePath = "C:\Users\name\Documents\Reports\Combined Tech Bonus and Pay Adj\PDFs\Tech Pay - " & Format([C6], "mm-dd-yyyy") & ".pdf"
 
Upvote 0
I also want the replace ID part though, the filename should include the tech name that gets selected by each sheet, and also the date that will always be the value in C6
 
Upvote 0
tempPDFFilePath = "C:\Users\name\Documents\Reports\Combined Tech Bonus and Pay Adj\PDFs\Tech Pay - " & rngListStart.Offset(i - 1, 0).Value & " - " & Format(ws.[C6], "mm-dd-yyyy") & ".pdf"
 
Upvote 0
Solution

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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