+ Reply to Thread
Results 1 to 4 of 4

How to iterate lines of excel to save as pdf w/ updated filename

  1. #1
    Registered User
    Join Date
    01-09-2020
    Location
    San Francisco
    MS-Off Ver
    Excel 2016
    Posts
    4

    How to iterate lines of excel to save as pdf w/ updated filename

    Hi guys,

    Beginner here, so sorry for the mistakes. I hope that this is not too much trouble. My goal is to go through each line of code in Excel Workbook, and ExportAsFixedFormat each line as a separate, distinct pdf, with a constantly-changing cell value as filename. I have been at this for about a week and a half. At first, I was using Selection to take each Row I click on and print that as PDF, but this is too hard. I have seen code that does this for one pdf, but I need a lot of different pdfs. I am trying to take the entire spreadsheet and go line by line now, taking a pdf for each row, while also trying to print the labels at the top.

    Here's an example:

    Name Weight
    Zeke 20
    Dylan 14

    And it would give, as two separate pdfs with filenames "Zeke" and "Dylan" respectively, with the content:

    Name Weight
    Zeke 20

    And the other would be

    Name Weight
    Dylan 14

    What I have so far is this (please do not run on your own stuff, as it currently will spam your printer to print blank pages).

    Screen Shot 2020-01-19 at 12.25.43 PM.png

    Right now, none of my code works (as mentioned, it prints a bunch of blank pages). I'm going to keep working on it, but I've sort of hit a wall. How do I PageSetup, or change what will be saved as a pdf, without having the macro just print the page out? I'm totally confused.

    Some ideas I have is using a separate worksheet, that I can use the "set" function to make it only contain the TitleRow and CounterRow of Worksheets("Sheet1"). Maybe I can use a for loop given a range, to go through each line until lastRow, if Counter is responsible for this total mess.

    Any help or guesses would be greatly appreciated, even to point me in a better direction. Thank you!

    -Qiu Ye

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to iterate lines of excel to save as pdf w/ updated filename

    Hello yequi,

    This worked for me. Just be sure to change the folder path to where you want the files saved.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    01-09-2020
    Location
    San Francisco
    MS-Off Ver
    Excel 2016
    Posts
    4

    Re: How to iterate lines of excel to save as pdf w/ updated filename

    Thank you so much. For my own clarity, I am trying to piece together what happened here below. I really can't thank you enough, Leith!

    -Create Folder path
    -Give a worksheet to work in
    -Set Rng as the CurrentRegion of Wks.Cells(1, "A") ... essentially meaning, the first cell of column A, but ensures that blank rows have no influence. Becomes important for the index For loop.
    -Set the Wks Name as "print_area" which will be the Name Object referred to with "RefersToRange", and Rng is the Range Object that will be called on.
    -Set the pdfRng, or the stuff that we want to print, as the value of 1, "A", and the offset, or the cell to the right. Need to use Intersect, as pdfRng is within Rng.
    -Is Nothing essentially checks for stuff like, is the value of the cell 0 or "", and handles that by exiting the macro ... and, if it is non-zero or non-"", hides the pdfRng, so that you can only reveal the row of data you want to print.
    -For loop indexs through each line of pdfRng, we make sure we go through each line by using the "Count" command
    -Only the indexed row is un-hidden
    -The File-name is set to the value of the indexed cell, using "Item", and is prepared to be a pdf
    -Returns Range object referred to by a Name object, the Range object is Rng and Name object is "Print_area," and Rng is essentially the value of Cell(1, "A"). This is then export as pdf
    -The indexed row is hidden. The "CurrentRegion" comes in handy here, since it will not pick up on the hidden row?
    -We index the next cell, effectively going through the whole spreadsheet
    -We unhide the rows at end, so that our data does not all get hidden after we execute the macro.

    Again, thank you sincerely.

    -Qiu ye

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to iterate lines of excel to save as pdf w/ updated filename

    Hello yequi,

    Rather than address your outline one line at time, I have fully annotated the macro to describe the actions in detail.

    Please Login or Register  to view this content.

+ 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. Save Excel sheets as PDF referncing filename
    By Alcotraz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-30-2015, 08:52 AM
  2. [SOLVED] Read out Filename and Save all Sheet as Filename.pdf
    By bluesurger in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-28-2014, 02:34 AM
  3. Replies: 0
    Last Post: 11-27-2012, 11:57 AM
  4. filename cell updated with the filename inputed in the dialogue box
    By pba.mike in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-27-2007, 08:15 AM
  5. Auto save at intervals to new filename in excel
    By Lokesh Patel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-02-2005, 06:20 PM
  6. Auto save at intervals to new filename in excel
    By Lokesh Patel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-02-2005, 06:17 PM
  7. Get the Excel Filename after a save
    By Remy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-16-2005, 07:05 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