+ Reply to Thread
Results 1 to 7 of 7

"Stuck" - Macro to Save Workbook

  1. #1
    Registered User
    Join Date
    10-22-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    65

    Cool "Stuck" - Macro to Save Workbook

    I have a command button that create PDFs of my worksheets. Before the PDF are to open I would like for the Workbook to be saved in a specific folder. I am not sure what I am doing wrong. Please help - I a first time user of Macros.

    Please Login or Register  to view this content.
    Thank you!!!

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: "Stuck" - Macro to Save Workbook

    The short answer is remove the single quote on your first line of code under the Sub statement. The single quote indicates a comment, which is ignored during code execution and just used as a way for programmers to put notes in the code.

    A longer answer is that it looks like you used the macro recorder to generate this code. I'm sure the code works perfectly fine but there are a number of settings that are redundant or unnecessary which clog up the code and could slow things down a bit. For example, in those With blocks you are setting many attributes to the default. You really only need to include the ones you care about. I am guessing that you only care about margins, for example. You also do the whole thing twice.

    Excel makes it very easy to record macros but delving into the code with no knowledge of how that code works is confusing at best. Depending on how much coding you want to do in the future, you might want to do a little research on the Excel object model and how VBA works (statements, functions, variables). This information is available in the Help pages in the VBA development window (click the ? icon) but it's not the most well-written documentation I've ever read and better as a reference than a tutorial. But there's lots of stuff online and some good books.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    10-22-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: "Stuck" - Macro to Save Workbook

    6StringJazzer,
    You have been extremely helpful. I removed the single quote and that allow it to work fine. I am really very new at this, so I appreciate your help and all your explaination. I will also take a look into the how VBA works to get a better understanding. Yes for the most part the above coding was done by recording and I am sorry I would not currently know how to simplify it. You are correct it is written twice because it is for two different sheets that need to be PDF. I will have to write it again two more time for two more sheets, each with it own name of the type of sheet they are. If that made sense.

    Once again Thank you - I will update post to solved.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: "Stuck" - Macro to Save Workbook

    I didn't mean that you did it for one sheet then again for another. I meant that there are redundant operations on the same sheet. See the excerpt below, scroll down to the code highlighted as blue.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-22-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: "Stuck" - Macro to Save Workbook

    Oh I see what you are saying now -- So if I delete the section you lighted in Blue, it should still work just fine? I will give it try and update the post for everyone to have this information on how it works. Thank you. Inez15

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: "Stuck" - Macro to Save Workbook

    Yes, if you delete the blue part everything should still work. Also, there are a lot of lines that explicitly set attributes to defaults, which the macro recorder gives you but you don't really need to do. For example, the blue code here is probably unnecessary:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-22-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: "Stuck" - Macro to Save Workbook

    I removed the information you gave it and it worked great. Thank you so much for showing me how to clean up my Macro.

+ 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