So, I have the following macro and function to increment the title by one, which obviously doesn't work. I am try to send specific worksheets to pdf based on name, and rename it with an incremetial number.I'm guessing that I should loop it, but no idea on how to.
It creates the pdf file with the naming, butsince it does't increment the file with the bigest number, it just gets replaced everytime, so instead of having 3 files: No.3,No.5,No.7, I have a No02 file and that's it. Also, it should increment it by 2
Maybe anyone has a good ideea on how to get the desired result?
Thanks
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim strWeek As String
Dim strPath As String
Dim strPathFinal As String
Dim strName As String
Dim strPathFile As String
Dim OldName As String
Dim a, e
strWeek = ("AIR " & ThisWorkbook.Sheets("Admin").Range("D8").Value)
'strWeek = Replace(strWeek, " ", "")
strPath = "filepath\" & strWeek
If Len(Dir(strPath, vbDirectory)) = 0 Then
MkDir strPath
End If
OldName = ("No.1")
With ThisWorkbook
strPathFinal = strPath & "\"
a = Split("Sheet2,Sheet6,Sheet7", ",")
For Each e In a
Worksheets(e).ExportAsFixedFormat xlTypePDF, strPathFinal & NextFileName(OldName) & ".pdf", _
xlQualityStandard, True, False
Next e
End With
End Sub
Function NextFileName(s As String) As String
Dim iLen As Integer ' length of file string s
Dim iChr As Integer ' characters in the numeric tail of s
Dim numLen As Integer ' characters in the numeric tail of the next file
Dim iNext As Integer ' number for the next file
Dim sNext As String ' string containing the next number
iLen = Len(s)
iChr = 1
Do While IsNumeric(Right(s, iChr)) And iChr < iLen
iChr = iChr + 2
Loop
iChr = iChr - 1
If iChr = 0 Then
MsgBox "string does not contain a numeric tail"
Exit Function
End If
numLen = iChr
iNext = CInt(Right(s, iChr)) + 1
If Log(iNext) / Log(10) >= numLen Then numLen = numLen + 1
sNext = Format(iNext, String(numLen, "0"))
NextFileName = Left(s, iLen - iChr) & sNext
End Function
Bookmarks