I have a code which gets data from a sheet and populates a form. Within the data there are duplicate entries. [Refer to Data img.]
Dim i As Long
Dim dataWS As Worksheet, formWS As Worksheet
Dim thisFile As Range, destRange As Range
Dim thisFile2 As Range, destRange2 As Range
FolderPath = "C:\Users\Lenovo\Documents\PAF_Output\"
MkDir FolderPath
Set dataWS = Sheets("Data")
Set formWS = Sheets("Form")
For i = 2 To 5
Set thisFile2 = dataWS.Range("A" & i)
Set destRange2 = formWS.Range("B4:I4")
thisFile2.Copy destRange2
Set thisFile = dataWS.Range("B" & i)
Set destRange = formWS.Range("O4:Q4")
thisFile.Copy destRange
Sheets(Array("Form")).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, FileName:=FolderPath & thisFile2.Value & ".pdf", _
openafterpublish:=False, ignoreprintareas:=False
Next i
End Sub
As you can see from
FileName:=FolderPath & thisFile2.Value & ".pdf"
The files are named after the values from column A. Nonetheless, in the case of the duplicate entries, excel will override the first file with the second one. What I’m trying to do is now create a name combining the names of the values within Column A and the arrival date values in Column B. Like this…
FileName:=FolderPath & thisFile2.Value & thisFile.Value & ".pdf"
This throws me an error though. Can someone help me please?
You need to format your date so the slashes (/) don’t appear, as filenames cannot contain these characters, as below, also worth mentioning that you are copying from a single cell and pasting into a range, which I doubt is what you wanted to achieve..:
Dim i As Long
Dim dataWS As Worksheet: Set dataWS = Sheets("Data")
Dim formWS As Worksheet: Set formWS = Sheets("Form")
Dim thisFile As Range, destRange As Range
Dim thisFile2 As Range, destRange2 As Range
FolderPath = "C:\Users\Lenovo\Documents\PAF_Output\"
MkDir FolderPath
For i = 2 To 5
Set thisFile2 = dataWS.Range("A" & i)
Set destRange2 = formWS.Range("B4:I4")
thisFile2.Copy destRange2
Set thisFile = dataWS.Range("B" & i)
Set destRange = formWS.Range("O4:Q4")
thisFile.Copy destRange
formWS.ExportAsFixedFormat _
Type:=xlTypePDF, fileName:=FolderPath & thisFile2.Value & Format(thisFile.Value, "MM-dd-yyyy") & ".pdf", openafterpublish:=False, ignoreprintareas:=False
Next i
End Sub
UPDATE:
To tidy up the code a little more and remove not needed statements, like the copy and paste, please see below:
Sub test()
Dim i As Long
Dim dataWS As Worksheet: Set dataWS = Sheets("Data")
Dim formWS As Worksheet: Set formWS = Sheets("Form")
FolderPath = "C:\Users\Lenovo\Documents\PAF_Output\"
If Dir(FolderPath, vbDirectory) = "" Then MkDir FolderPath
'above if the folder doesn't exist then create it
For i = 2 To 5
formWS.Range("B4:I4") = dataWS.Range("A" & i)
formWS.Range("O4:Q4") = dataWS.Range("B" & i)
'above transfer the values from one range to another without copying
formWS.ExportAsFixedFormat _
Type:=xlTypePDF, fileName:=FolderPath & thisFile2.Value & Format(thisFile.Value, "MM-dd-yyyy") & ".pdf", openafterpublish:=False, ignoreprintareas:=False
Next i
End Sub
Tags: excelexcel, pdf, vba