You need to include the defining of the variable PDFFolder in the loop as follows
Sub URLToPDF()
Dim PDFFolder As String
Dim LastRow As Long
Dim arrSpecialChar() As String
Dim dblSpCharFound As Double
Dim PDFPath As String
Dim i As Long
Dim j As Integer
'An array with special characters that cannot be used for naming a file.
arrSpecialChar() = Split("\ / : * ? " & Chr$(34) & " < > |", " ")
'Find the last row.
With Worksheets("Main")
.Activate
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
End With
'Check if there is at least one URL.
If LastRow < 8 Then
MsgBox "You did't enter a URL!", vbCritical, "No URL"
Exit Sub
End If
'Convert the URLs to PDFs.
For i = 8 To LastRow
On Error Resume Next
PDFPath = Cells(i, 4).Value
PDFFolder = Cells(i, 2).Value
If Right(PDFFolder, 1) <> "\" Then
PDFFolder = PDFFolder & "\"
cells(i,2).value = PDFFolder
End if
'Check if the PDF name contains a special/illegal character.
For j = LBound(arrSpecialChar) To UBound(arrSpecialChar)
dblSpCharFound = WorksheetFunction.Find(arrSpecialChar(j), PDFPath)
If dblSpCharFound > 0 Then
PDFPath = WorksheetFunction.Substitute(PDFPath, arrSpecialChar(j), "-")
End If
Next j
PDFPath = PDFFolder & PDFPath
On Error GoTo 0
'Save the PDF files to the selected folder.
Call WebpageToPDF(Cells(i, 3).Value, PDFPath & ".pdf")
Next i
'Inform the user that macro finished.
MsgBox LastRow - 7 & " web pages were successfully saved as PDFs!", vbInformation, "Done"
End Sub
Bookmarks