+ Reply to Thread
Results 1 to 11 of 11

VBA saving worksheet as PDF with added numerical increments

  1. #1
    Registered User
    Join Date
    07-09-2021
    Location
    United Kingdom
    MS-Off Ver
    10
    Posts
    13

    VBA saving worksheet as PDF with added numerical increments

    Hello lovely community

    I have come across an issue that is seriously holding me back on VBA.

    I have created a sub below to convert a worksheet into a PDF file in a specific location, with the current date and specific name of the person the file is about that will be displayed in cell B3 of the worksheet. Additionally, i have attempted to add an increment on the end, (1, 2, 3) should there be multiple files saved during a day. However, i keep encountering 2 errors.

    Issue 1:
    The increment is working but the file is not saving as a PDF.

    Issue 2:
    Strangely, only the first part of the persons name is saved as the file name in the folder. It needs to be saved as the full B3 cell value, even if there is a space between words in cell B3.

    I have included the code below. Your help would be much appreciated.
    Thank you
    Betty

    Please Login or Register  to view this content.
    Last edited by BettyMay; 09-20-2021 at 08:02 AM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: VBA saving worksheet as PDF with added numerical increments

    To save a worksheet as PDF take a look at ExportAsFixedFormat-method.

    Be sure there are no invalid characters in your filename. For instance change dots in your date to underscores.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: VBA saving worksheet as PDF with added numerical increments

    Your end code marker should be [/CODE]

    Issue 1 you are not asking to save as PDF, usually this is an export see
    https://powerspreadsheets.com/save-excel-file-pdf-vba/ for exactly what you want to do

    we can not see the sheet or the values of B3 so it is hard to advise

  4. #4
    Registered User
    Join Date
    07-09-2021
    Location
    United Kingdom
    MS-Off Ver
    10
    Posts
    13

    Re: VBA saving worksheet as PDF with added numerical increments

    Thanks davsth,

    B3 is a FirstName SecondName value, with the space in between.


    I've tried to input Export As PDF instead of the 'SaveAs, Filename' at the bottom of the code but it then ignores the rest of the naming instructions?

    Thanks for your help

  5. #5
    Registered User
    Join Date
    07-09-2021
    Location
    United Kingdom
    MS-Off Ver
    10
    Posts
    13

    Re: VBA saving worksheet as PDF with added numerical increments

    I have tried to use the Export As Fixed Format, but i'm struggling to get it right.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: VBA saving worksheet as PDF with added numerical increments

    Well post a sample as per the yellow banner and you might get a better answer

    try and find step through the code with f8 and find the values of
    Len(Sheets("TempKIT").Range("B3").Value)

    eg check1=Len(Sheets("TempKIT").Range("B3").Value)
    and check2 =Sheets("TempKIT").Range("B3").Value

    You can see these in the locals window

  7. #7
    Registered User
    Join Date
    07-09-2021
    Location
    United Kingdom
    MS-Off Ver
    10
    Posts
    13

    Re: VBA saving worksheet as PDF with added numerical increments

    Hi there,

    Please see attached sanitised workbook.

    Thank you so much for your help

    Betty
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: VBA saving worksheet as PDF with added numerical increments

    try the below it works for me
    Please Login or Register  to view this content.
    although the path needs to be changed to H:\HR for Managers\1. Absence and Welfare\Active NOAs\ from u:\ this is where I tested it, any closer?

    but it works for me.

    Also on your profile the office version is 365 or 2019, 2016, 2013 etc 10 is your windows version. At some point you will be asked to change it by an admin!
    Last edited by davsth; 09-20-2021 at 09:35 AM.

  9. #9
    Registered User
    Join Date
    07-09-2021
    Location
    United Kingdom
    MS-Off Ver
    10
    Posts
    13

    Re: VBA saving worksheet as PDF with added numerical increments

    Sorry, still new to the forum.

    Thank you it works! I just had to Dim nametosave as a String and its working perfectly.

    I can see where i was going wrong as well, so i really appreciate your efforts. I'm just looking into why it cutes off the second half of B3 value. Ws it doing that for you?

    Have some reputation

  10. #10
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: VBA saving worksheet as PDF with added numerical increments

    it was cutting off the last four characters as this is what you wanted to do? If you didn't
    nametosave = "u:\" & "KIT Form" & " " & Sheets("TempKIT").Range("B3").Value & " " & Format(Date, "dd.mm.yyyy") & " " & IIf(n = 0, "", n) & ".pdf"

    would have worked. but you never said what you wanted the value to be, this could be a naming convention in your company for all we knew!

    Glad you got there!

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: VBA saving worksheet as PDF with added numerical increments

    As already mentioned, what are you trying to achieve with this

    Please Login or Register  to view this content.
    Also this & ".pdf" is not needed in your savaename because when saving as PDF, the extension is automatically added.

+ 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. [SOLVED] Duplication with numerical increments
    By Llewellyn1972 in forum Excel General
    Replies: 5
    Last Post: 04-24-2019, 03:25 AM
  2. A macro that automatically increments the first cell when a new row is added
    By t1mmclaren in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-04-2015, 09:36 PM
  3. Replies: 6
    Last Post: 04-01-2013, 11:18 AM
  4. Replies: 7
    Last Post: 12-16-2011, 08:41 AM
  5. Saving newly added workbooks
    By xlsnovice in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2010, 04:01 PM
  6. Replies: 2
    Last Post: 04-23-2010, 05:54 AM
  7. Replies: 9
    Last Post: 01-07-2009, 09:02 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