+ Reply to Thread
Results 1 to 6 of 6

Export specific sheets to individual PDFs and rename each one adding an incremential no

Hybrid View

  1. #1
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Export specific sheets to individual PDFs and rename each one adding an incremential no

    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
    Click the * to say thanks.

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Export specific sheets to individual PDFs and rename each one adding an incremential n

    Please upload a sample file to make use of....
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Export specific sheets to individual PDFs and rename each one adding an incremential n

    Please find attached. Also, I already have a file named No.1 in the folder
    Attached Files Attached Files

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Export specific sheets to individual PDFs and rename each one adding an incremential n

    So as I understand it...

    You want to create a Folder called AIR12 and save specific sheets to this folder named No02, No05, No07 ???
    Option Explicit
    
    Sub PDFNum()
    Dim ShtArr, i As Long, strWeek As String, Path As String, num As Long
    Application.ScreenUpdating = False
    ShtArr = Array("Sheet2", "Sheet6", "Sheet7")
    strWeek = "AIR " & Sheets("Admin").Range("D8").Value
    num = 1
    Path = ThisWorkbook.Path & "\" & strWeek
    If Len(Dir(Path, vbDirectory)) = 0 Then
        MkDir Path
    End If
    For i = LBound(ShtArr) To UBound(ShtArr)
        With Sheets(ShtArr(i))
            .ExportAsFixedFormat xlTypePDF, Path & "\" & "No." & Format(num + 2, "00") & ".pdf", xlQualityStandard, True, False
            num = num + 2
        End With
    Next i
    Application.ScreenUpdating = True
    End Sub
    Last edited by sintek; 07-31-2019 at 06:56 AM.

  5. #5
    Forum Expert PaulM100's Avatar
    Join Date
    10-09-2017
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    2,108

    Re: Export specific sheets to individual PDFs and rename each one adding an incremential n

    So easy and so simple, sintek. Thank you very much

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Export specific sheets to individual PDFs and rename each one adding an incremential n

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Thanks.gif

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Export Sheets Selected in ListBox as separate PDFs
    By abulkhairi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2017, 09:33 AM
  2. Save range of worksheets as individual PDFs
    By sgaucho in forum Excel General
    Replies: 0
    Last Post: 05-25-2017, 05:58 PM
  3. Looping through name range and create one pdf instead of individual pdfs
    By igoodable in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-10-2016, 08:13 AM
  4. Loop to Save Tab as Individual PDFs
    By CDNcameron in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-30-2015, 06:59 PM
  5. VBA export sheets as PDFs, merge with existing PDFs
    By wannabeexcelguy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-26-2015, 07:32 PM
  6. [SOLVED] Copy a sheet, rename it by value in range then export product to new workbook and rename
    By MagicMan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-31-2015, 07:24 PM
  7. [SOLVED] Save Multiple Worksheets as Individual PDFs
    By jmk8602 in forum Excel General
    Replies: 3
    Last Post: 02-26-2014, 01:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1