+ Reply to Thread
Results 1 to 7 of 7

Creating Loop that creates and saves specific sheets(tabs) to PDF

  1. #1
    Registered User
    Join Date
    04-08-2011
    Location
    London
    MS-Off Ver
    O 365
    Posts
    80

    Creating Loop that creates and saves specific sheets(tabs) to PDF

    I'm trying to create a loop that will pdf selected sheets that are between sheets "Start" and "End". For some reason it will pdf the first tab but then it stops. Here is my current code:

    Sub PDF_Converter()

    Dim ws As Worksheet, aws As Worksheet
    Dim StartIndex, EndIndex As Integer
    Dim i As Integer
    Set aws = ActiveSheet
    MName = ActiveSheet.Name & ".pdf"
    MDir = "U:\Dashboard\Jaime Bailey"
    StartIndex = Sheets("Start").Index + 1
    EndIndex = Sheets("End").Index - 1
    Application.ScreenUpdating = False

    Sheets("Start").Next.Select
    For i = StartIndex To EndIndex


    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=MDir & "\" & MName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    Next i

    End Sub

  2. #2
    Forum Contributor
    Join Date
    02-12-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    275

    Re: Creating Loop that creates and saves specific sheets(tabs) to PDF

    You need to change the sheet and use the i inside you your loop to identify the sheet to be exported



    PS: Remember to use the [code] tag when quoting code.

  3. #3
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Creating Loop that creates and saves specific sheets(tabs) to PDF

    Instead of ActiveSheet.ExportAsFixedFormat....

    Please Login or Register  to view this content.
    Post responsibly. Search for excelforum.com

  4. #4
    Registered User
    Join Date
    04-08-2011
    Location
    London
    MS-Off Ver
    O 365
    Posts
    80

    Re: Creating Loop that creates and saves specific sheets(tabs) to PDF

    Thanks Chippy but for some reason it's still only saving out the first tab within the parameters. For some reason it's not looping back through and doing the other following tabs. Here is what I have after using your last update:

    Sub PDF_Converter()

    Dim StartIndex, EndIndex As Integer
    Dim i As Integer
    MName = ActiveSheet.Name & ".pdf"
    MDir = "U:\Dashboard\Jaime Bailey"
    StartIndex = Sheets("Start").Index + 1
    EndIndex = Sheets("End").Index - 1
    Application.ScreenUpdating = False


    For i = StartIndex To EndIndex


    Sheets(i).ExportAsFixedFormat Type:=xlTypePDF, Filename:=MDir & "\" & MName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    Next i

    End Sub

  5. #5
    Forum Contributor
    Join Date
    02-12-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    275

    Re: Creating Loop that creates and saves specific sheets(tabs) to PDF

    Cherpy has a good line there but try this one as addition to your original code

    Please Login or Register  to view this content.

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Creating Loop that creates and saves specific sheets(tabs) to PDF

    dash11,

    It is looping through and saving the sheets, the problem is that each time it saves, it is saving as the exact same name, so it just keeps overwriting the file. You need to put your MName line inside the loop:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  7. #7
    Registered User
    Join Date
    04-08-2011
    Location
    London
    MS-Off Ver
    O 365
    Posts
    80

    Re: Creating Loop that creates and saves specific sheets(tabs) to PDF

    Thanks All! Tiger hit this one on the head. I had a loop setup that was chasing it's own tail. Much appreciated gentlemen!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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