+ Reply to Thread
Results 1 to 11 of 11

Wrote code to print worksheets based on numbers in cells and rows, but is it efficient?

  1. #1
    Registered User
    Join Date
    11-08-2013
    Location
    Sacramento, California
    MS-Off Ver
    Excel 2010
    Posts
    25

    Wrote code to print worksheets based on numbers in cells and rows, but is it efficient?

    Hello, I started learning VBA for the first time a week ago so please forgive my lack of knowledge. I am having a lot of fun with the added functionality.
    This is the first piece of code I am trying to write from scratch as well.

    My workbook has customers who's inputs will each fill out different forms but some of them will share the same forms (templates). My goal with this code is to have a workbook which prints specific sheets based on numbers (or references to sheets) in the cells next to the customer's name. The customer is picked by a dropdown menu (Named that range "Customer"). This data will be in a data sheet hidden later on. The reason for this, is that I want it to be easy for someone else to add a sheet later on and have it print for the specific customer simply by adding the number to the list next to their name.

    This is what I came up with and it seems to work! But, being so new, I don't know if I am doing this as efficiently as I could be. Moreover, I am having trouble understanding how not to use the .select and .activate methods. I know I am supposed to work with Objects directly, but in some cases where I used .select I can't think of how not to use it.

    Please Login or Register  to view this content.
    It took me a while to figure out how to get all prints to be one job and for the selection to NOT include the data sheet as well. Lastly, I am having trouble understanding how to use arrays. Should I be using an array for this instead? I just want make sure I am creating something robust and efficient. I am happy to be a part of this forum and I definitely appreciate everyone's time and help.

    I tried uploading the workbook, but it continually tries to upload without actually doing it. (Maybe my work network or something? It's only a 37KB .xlsm)

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    Hi,

    You could try using dropbox or something else to add your file. Its better to debug and work with if I have the actual workbook (I am not an expert)

  3. #3
    Registered User
    Join Date
    11-08-2013
    Location
    Sacramento, California
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    Hi, Thanks for your response! I turned off enhanced uploading and it worked! I was using multiple modules to try different things, but the one I am using for this code is Module 4.


    EXPORT PROJECT TEST BOARD.xlsm

  4. #4
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    Hi,

    See if this helps.

    Please Login or Register  to view this content.
    By the way this code uses another loop for the sheets because you are using numbers alone for the actual name of the sheet and it conflicts with the sheet index.

    Thanks

  5. #5
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    In this example the sheets are name S1, S2... and so on instead of 1,2,.. so you can get rid of that second loop that would slow you down

    Please Login or Register  to view this content.
    Thanks

  6. #6
    Registered User
    Join Date
    11-08-2013
    Location
    Sacramento, California
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    Thanks for the reply! I tested out the Sub and while it does print the sheets in the range, it prints out the whole currentregion. The difference from mine is that mine only prints sheets for the customer that has been selected in the drop down box and also mine makes it only one print job (which is my goal as well).

    However, you code does show me that I can probably remove one of my For Each loops (which loops through sheets) and replace it with an IF THEN with Sheets.Printout.
    The way I got it to be one job is by using an IF THEN and Select or Select (false) to make a selection of the sheets and then print selection.

    So maybe if I do and IF THEN with Sheets.Select / False then print I can reduce the amount of code...

    Thanks for your help!

  7. #7
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    I am sorry I forgot about that. This should do it
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-08-2013
    Location
    Sacramento, California
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    Hi Fredlo,

    Thank you, I appreciate the effort. Unfortunately, this code still prints the sheets as different print jobs, but it does a good job of selecting which customer criteria to print.
    I will definitely take from that, however, my code still has fewer variables. I'm not sure if that is really good or bad tho, because my code uses active and select more, which I am still confused about. Is there a way my code could be re-written to not use .Select and .Activate? That way I could better understand how to avoid it.

    Thanks!

  9. #9
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    Hi,

    I am not an expert but Look at what I point out in my code

    Please Login or Register  to view this content.

    Thanks

  10. #10
    Registered User
    Join Date
    11-08-2013
    Location
    Sacramento, California
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    I see, that helps me to understand much better! Thank you!

  11. #11
    Registered User
    Join Date
    11-08-2013
    Location
    Sacramento, California
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Wrote code to print worksheets based on numbers in cells and rows, but is it efficient

    So, in looking at your code. How would you get this part to print as one job?

    Please Login or Register  to view this content.
    Basically, why I felt I had to use .Select here is that the For Each loop goes through all the worksheets and when the condition fits it prints. To me .select was the only thing I could think of that could essentially store the sheets in a "staging area" until they were all chosen and ready to print.

+ 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. VBA code to print rows based on value in select column
    By deadeyesky in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2013, 10:50 AM
  2. 2010 VBA Code for: format the print area to include only rows with populated cells
    By sambo24 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-19-2013, 10:57 AM
  3. [SOLVED] More efficient code for lookup based on cell value
    By hilltop804 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-07-2013, 10:34 AM
  4. Replies: 0
    Last Post: 05-14-2013, 05:12 PM
  5. Creating efficient code: copy/paste between worksheets
    By yunesm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-21-2010, 11:37 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