+ Reply to Thread
Results 1 to 20 of 20

How to create a loop?

  1. #1
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    How to create a loop?

    I have a portion of code from a workbook with pages and pages of code that interacts with several other workbooks.

    In this section of code for a worksheet, I have 2 cells and people input code values of defects in either 1 or both cells. The cells are populated by pushbuttons on a userform.

    The codes individuals can choose are a predetermined list on the userform... and they can only fill out the worksheet with up to 2 codes.

    the prepared worksheet (after user form code is process) is copied and that page with the choosen codes entered is sent downstream via outlook.

    Hopefully that all makes sense.

    The issue I have is that that I was brand new to VBA when I first wrote the code and now I have more experience I think this can likely be done with less code.
    There are 32 code options and each option is coded as below with just changing the number from 1 to 32 ... can't I create something called a loop to make the code not be so extremely long?? Each of the "subs" is a seperate click button ( i.e. 32 clickable buttons for this portion of code on the userform)

    Please Login or Register  to view this content.
    Last edited by LiLi1; 11-22-2011 at 11:30 AM. Reason: code tags not displaying correctly

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: How to create a loop?

    Hi

    One of the ways to do this is build a class module to collect all the controls on a form of a certain type, and allocate events to them. There are examples on this site. here's one

    See if you can follow this, or others like it through to get you going.

    If this gets you lost (believe me, first few times through, I was completely lost), then build an example file, with a form, any relevant code etc that replicates your work file for the part you want to recode, and we can work with that to get you going.

    HTH

    rylo

  3. #3
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    I tried to read through the link and googled the concept but was pretty lost. I am pretty much self taught by just monkeying around with code examples from net examples and books and seeing others code etc. I haven't ever had a computer class or anything really.

    It's hard to do an example file because this one is so huge. I tried to purge the majority of the code and only keep revelant information. The file is kind of buggy but I hope the intent is there enough that my question of how to streamline the code somewhat can be answered.
    Last edited by LiLi1; 11-20-2011 at 07:36 PM.

  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 create a loop?

    Hello LiLi1,

    An alternative to the Class module is create a single Sub that can be used by all of the buttons. The code below replaces the button code you now have.

    The way it works is by extracting the buttons sequence number (1 to 32) from the button's name. This value is then inserted into the appropriate error box.
    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!)

  5. #5
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    Thanks so much for the reply! Much appreciated... couple of questions

    I have had several people complain that the file takes almost a min. or so to run and it used to take about 20 secs ( not sure how reliable the times are)

    So my intent to change the code was to make the file run faster.... it seems like there is still alot of code... will the changes make the file run faster? Sorry if this is obvious I don't understand a lot about VBA yet.

    P.S.
    Any relation to the popular pet website LeithPetwerks??

  6. #6
    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 create a loop?

    Hello LiLi1,

    The changes I made will not significantly impact the execution time of the macro. It does however condense the code and make it easier to manage.

    No relation to the pet website. Leith is not a common name in the US. There are only about 4,000 people who have the name as forename or surname. The numbers are higher in Scotland.

  7. #7
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    Thanks... Are there typical things that do influence the execution time ( I don't tend to use a lot of Dim statements and I wonder if I start trying to so more it will increase speed.... they tend to error out my code a lot when I try them and I wonder if it's because I tend to pass data around to a lot of different worksheets/workbooks ...that may use the data in different ways)

  8. #8
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    Also what does RegExp mean?

  9. #9
    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 create a loop?

    Hello LiLi1,

    In VBA is it seldom necessary to select an object before performing an action on it. For example, changing the value of a cell.
    Please Login or Register  to view this content.

    There are other ways to do this also. This is just one that will make a difference in execution speed.
    Last edited by Leith Ross; 11-20-2011 at 08:28 PM.

  10. #10
    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 create a loop?

    Hello LiLi1,

    RegExp is short for "Regular Expression". This is powerful scripting language that is used for string parsing and pattern matching.

  11. #11
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    Thanks... someone several months ago posted that about something else so I have tried to eliminate the select option but still do so occasionally... why does my code seem to work with clearing contents with merged areas sometimes with the code I posted without selecting and other times I have to select it? It's always been a mystery why sometimes it works and others it does not.

  12. #12
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    Okay I got the code posted in the correct place in the actual file and tried to run it... it doesn't work!

    It populates #'s in the displaycode boxes but they aren't the numbers that match the button pushed??

    It appears to work fine for #'s 1-9, but after that it truncates the first digit off of the # in the display box

  13. #13
    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 create a loop?

    Hello LiLi1,

    When referencing an object, like a Range, on a worksheet, if the object is not qualified then the active sheet is assumed.

    Let's say that "Sheet3" is the ActiveSheet.
    Please Login or Register  to view this content.

    This will clear the contents of "A1:A10" on "Sheet3". If the user changes sheets before the macro is run to "Sheet1" Then he macro will clear the contents of the cells "A1:A10" on "Sheet1".

    To ensure that only the cells "A1:A10" on "Sheet3" are cleared, the code can be written this way. By making the code reusable, your code will be easier to read, maintain, and provide you with more options. If you perform a function 3 or more times in your code, you should consider making it a separate procedure.
    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    Trying changing to this ... (seemed to be a good spot to tweek LOL)

    Set RegExp = CreateObject("VBScript.RegExp")
    RegExp.Pattern = "^.+(\d+)$$"

    Not a win however

  15. #15
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    This got it spitting out more #'s but none that make sense

    Please Login or Register  to view this content.

  16. #16
    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 create a loop?

    Hello LiLi1,

    I don't know what i was thinking. Change the ErrorCheck routine to what is below.
    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    Works Wonderful... Thanks Thanks Thanks!
    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    I am begining to think I am going to qualify for longest thread ever so I hate to even ask .. but that takes care of reduces the code for the displaying the box code what about the coloring them part? Can I also get rid of typing the code 32 times for the color? The color used to "highlight" the cells is always the same yellow the only thing in this portion of code that changes is where the 10 is currently it goes from 1 to 32. These are not individual subs ... it's the form where it pastes the info from the cells... so all the code just runs one after the other ... perhaps I really have stumbled onto a true loop question LOL ... it's amazing how much you can learn in each thread I post. I feel as if I have come full circle

    Please Login or Register  to view this content.
    Last edited by LiLi1; 11-20-2011 at 09:09 PM. Reason: code tags

  19. #19
    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 create a loop?

    Hello LiLi1,

    The cells color can be changed by using the DisplayBoxCodes as an index into a Collection object. This allows for random access to the cells. This is much faster than testing each condition.

    The collection is declared Public in the UserForm. The UserForm_Initialize event loads it up with the cells and the DisplayBoxCodes (1 to 32). When SendRFTEmail is clicked, the textbox values are checked and the appropriate cell is colored. The attached workbook has all the changes made.
    Attached Files Attached Files

  20. #20
    Forum Contributor
    Join Date
    01-14-2010
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: How to create a loop?

    Looks amazing thanks.. I *think* I am done

    I made all the changes and it works fine.. I however haven't turned all the code back on yet... will try it tomorrow and see if everything works together... if so I can hopefully mark it solved.

    Thanks Thanks ... Appreciated beyond words. Every time I work on a new project I think "man I am beginning to understand this VBA stuff" and yet I still walk away everytime going "yep I don't have a clue yet" LOL

+ 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