+ Reply to Thread
Results 1 to 29 of 29

Export PDF, File Name Cell on Another Worksheet

  1. #1
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Export PDF, File Name Cell on Another Worksheet

    Hi There,

    I am working on a script to export two separate worksheets to separate PDF files in a specific folder, with the file name as a cell referecen on the active sheet for the first printout and the filename of the "next" worksheet as the same filename with _Lab added to the file name.

    Part 1 is working perfectly:

    Please Login or Register  to view this content.
    Part 2 is working perfectly to add the _Lab component still on the active sheet:

    Please Login or Register  to view this content.
    When I try to have it move to the next worksheet, add the _Lab but still refer back to the previous worksheet for the file name beginning, i get errors:

    Please Login or Register  to view this content.
    What am I doing wrong?
    Les

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Export PDF, File Name Cell on Another Worksheet

    If you change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    or this
    Please Login or Register  to view this content.
    does that make a difference?
    Last edited by jolivanes; 04-03-2019 at 06:38 PM.

  3. #3
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    Both of them give me errors. Is it possible this portion is the issue?

    Please Login or Register  to view this content.
    Worksheets(ActiveSheet.Index + 1).Select

    I found this piece online and haven't had it working yet. Is there a better way to move to the next sheet before the second PDF is created?

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Export PDF, File Name Cell on Another Worksheet

    You never said what the error was but where is your
    Please Login or Register  to view this content.
    after you select the next indexed sheet?
    BTW
    Please Login or Register  to view this content.
    will only error when the last Sheet is the active Sheet

  5. #5
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    The worksheets are back to back. The first one allows the user to paste in a record number and self populates. This is when I want the user to be able to hit a button which will export a pdf of the active sheet named the cellreference.pdf THEN export the next worksheet as a PDF called cellreference_Lab.pdf

    Does that answer your question? I'm kind of a inexperienced and not quite sure how to determine where the error is. When I run the Macro it gets a Compile error: Invalid or unqualified reference and highlights

    Please Login or Register  to view this content.
    .ExportAsFixedFormat

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Export PDF, File Name Cell on Another Worksheet

    Attach a sanitized copy of your file so we can have a check.

  7. #7
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    Here is a mock up.

    Basically I want to be able to export the first print tab to a PDF in mypictures/current month. The filename should be the range of B3 contents.pdf.

    The second export should take place to the same folder, to the same cell range name but have B3 contents_Lab.pdf

    Does that help?
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Export PDF, File Name Cell on Another Worksheet

    You can try the attached.
    Note the info in the TextBox.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    Good morning,

    Thanks for all of your help. When I edit with my path, it seems to work but after the bar pops up saying its publishing, then it crashes with the following error. Your code, edited with my path is below. Did I miss something?

    Please Login or Register  to view this content.
    Attachment 618862

  10. #10
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Export PDF, File Name Cell on Another Worksheet

    You asked about this code:
    Please Login or Register  to view this content.
    What's the content of F1 and you also mention Template!D1


    Instead of the value Acivesheet you can also set the specific sheet

    Please Login or Register  to view this content.
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  11. #11
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Export PDF, File Name Cell on Another Worksheet

    What's wrong with Jolivane's code?

  12. #12
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    It works in the mockup perfectly but as soon as I add my path it crashes.

    I'm sorry if I'm confusing everybody. My issue is when someone says add your path here, I'm novice enough that I "hope" I am copying and pasting in the exact correct portion of my path into the exact correct spot in the code.

  13. #13
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Export PDF, File Name Cell on Another Worksheet

    Check if the syntax to your path is valid, that's something only YOU know.
    run the code line by line and when you hit the error hove above the path text and see what is tells you.
    What does Environ("USERPROFILE") look like in the immediate window you can type a ? Environ("USERPROFILE") and strike enter and you'll see where it pits to.
    Does the Pictures foldre exist, does current month exist?

  14. #14
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    Let me summarize the portion of the issue I am trying to do. I have code that works successfully to export a PDF file into the correct folder. It works perfectly but the file name is only _Lab with no record number. The code that is currently working is below. I can either keep the sheet active and refer to the other sheet for the filename only, or I need to rework the code to include the worksheet names as you mentioned above.


    -sheet 1 is called "Template", and has a field where a record number is pasted in cell D11
    -sheet 2 is called "Lab Template" updated automatically, is a locked sheet, it has the record number in cell E1 but this cell is a formula referring back to
    sheet 1
    -the script needs to export the sheet as a PDF and be named as the record number in D11 on the Template sheet e.g. record#_Lab
    - the code Jolivanes is perfect except that I cant keep his workbook and so I need to add the path to save it which would be in My Pictures/Current Month for any user of the workbook regardless where they are on the network.


    I hope that helped to clarify and not to confuse futher.

  15. #15
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    The pictures folder exists, as does the current month folder. If I F8 through the code line by line it crashed on the line:

    Please Login or Register  to view this content.
    here is the entire code I am running when that happens:

    Please Login or Register  to view this content.

  16. #16
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Export PDF, File Name Cell on Another Worksheet

    What if you unlock your "Lab Template" Sheet?

  17. #17
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    Oh just saw your question about the immediate window. When I question it I get this except "myprofile" is actually my name

    ?Environ("USERPROFILE") & "\Pictures\current month\"
    C:\Users\myprofile\Pictures\current month\

    I would say the path portion is working fine.

  18. #18
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    I have it unlocked currently. I thought that might be causing issues too.

  19. #19
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Export PDF, File Name Cell on Another Worksheet

    Okay debugging is the most timeconsuming of coding

    let's digest and you are the one to do it:

    Please Login or Register  to view this content.
    I suggest you add the following:


    Please Login or Register  to view this content.
    The result of Debug.Print is shwon in the imamediate window, you can als replace DEbug.Print with MsgBox

  20. #20
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    OK great. I was just watching a video on this ha ha. Here is what I get in immediate when I run that through:

    Please Login or Register  to view this content.
    Not sure what that last line is....

  21. #21
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    oh!!! is it possible that because my actual document has 5 worksheets and the 2 we are working on are sheet 3 and 4?

    I guess not. The extra worksheet I have on the actual workbook is at the end.
    Last edited by bigtunelover; 04-05-2019 at 12:13 PM.

  22. #22
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Export PDF, File Name Cell on Another Worksheet

    The forward slash is an invalid character when it comes to filenames.

  23. #23
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    I don't even know what that last line with the forward slash is referring to or where it comes from. It doesn't seem to have anything to do with the script.

  24. #24
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    OK I got it to work! Its now just exporting the 4th and 5th worksheet instead of the 3rd and 4th.

    What do I edit to back that up by one?

  25. #25
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Export PDF, File Name Cell on Another Worksheet

    Which Sheet is the Active Sheet (=visible on your desktop) when you run the code?
    The code in the attachment I supplied should save the Active (=visible) Sheet and the next Sheet to the right as PDF.
    If it now saves Sheets 4 and 5 you are running it when Sheet 4 is active I would think.

  26. #26
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Export PDF, File Name Cell on Another Worksheet

    The text is read into the array you're using so it must come from somewhere, VBA is not thát intelligent to invent things

  27. #27
    Registered User
    Join Date
    01-27-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2016, O365
    Posts
    97

    Re: Export PDF, File Name Cell on Another Worksheet

    I'm so confused. Here is where I am at. I think part of the problem is the script crashes when the file already exists in that folder. I'm not sure what to do about that but for not I have been deleting the created files before trying to run it again. That moves the script along, it saves the first file and appears to WANT to name the second file correctly but it still crashes.

    Here is the current code:

    Please Login or Register  to view this content.
    Here is the current immediate window:

    Please Login or Register  to view this content.
    The only place in the entire workbook the words "Quality Check" exist are on the template worksheet in cell P2. I can't see any reference to that cell in the Macro. I'm lost.

    So thankful for your patience.

  28. #28
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Export PDF, File Name Cell on Another Worksheet

    Start again, simple:
    We can run around in circles and keep saying 'I'm confused' and more of that, get to the point and then we can test.
    About a file already present then add a line to kill the file before exporting again.

    So be very clear (again) please:
    What is the buildup of the filename, you keep mention D11 but the record number is in B3.
    Come on try better

  29. #29
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Export PDF, File Name Cell on Another Worksheet

    Like I said I don't see which D11 value but this code will do for the sheets containing the test " Print" in the tab
    Please Login or Register  to view this content.
    The resulting pdf are attached

    Hope this goes better
    Attached Files Attached Files

+ 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. Excel 2016 VBA to Export worksheet to CSV file on Mac OS
    By MagdyAbdraboh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2018, 11:02 AM
  2. [SOLVED] Export data from worksheet to existing text file
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-20-2016, 07:12 AM
  3. Export a specific worksheet to another excel file.
    By paradoxofthecat in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-01-2015, 10:16 AM
  4. Export worksheet into new workbook and save file
    By thoseguys in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2013, 04:30 AM
  5. Export Each Column on Worksheet as Text File
    By ruezo in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-26-2012, 11:33 AM
  6. how do I export a worksheet as a fixed format ascii file
    By SVANATTA65 in forum Excel General
    Replies: 2
    Last Post: 06-16-2005, 07:05 PM
  7. [SOLVED] Macro to export a worksheet and save as new file
    By Mike_M in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2005, 05:50 AM

Tags for this Thread

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