+ Reply to Thread
Results 1 to 15 of 15

Loop problem

  1. #1
    Registered User
    Join Date
    08-03-2019
    Location
    Illinois
    MS-Off Ver
    Mac 14.7.7
    Posts
    17

    Loop problem

    Hi again,

    I'm still working through this reformatting project and trying to solve on my own with tutorials and a little help from you all. I'm still missing something here - probably syntax. In this instance I am trying to pull the first name out of a column from full name separated by a comma. It works fine most of the time but sometimes there is a company name in the row instead of a person's name so there is no comma. In that instance I would just like it to do nothing and move on to the next row. The code below works until it hits a cell without a comma and then it stops looping. Do I have 'Next DI' in the wrong place? How do I force it to continue looping until the end? Thanks in advance.

    Please Login or Register  to view this content.

  2. #2
    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: Loop problem

    Hello Jadee77,

    You need to change First to Dl on your Instr statement...
    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!)

  3. #3
    Registered User
    Join Date
    08-03-2019
    Location
    Illinois
    MS-Off Ver
    Mac 14.7.7
    Posts
    17

    Re: Loop problem

    THANK YOU! Still learning. I really appreciate the help.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop problem

    This question really should be a FAQ. I see it time and time again and looping just isn't the best way of doing this sort of thing.

    Whenever you find yourself creating looping code stop and think is this really necessary? Most of the time it isn't. Loops are inherently slow when they have to jump back and forth between VBA and the Excel application - there's a big time overhead at each pass through the loop.

    In this case use something like

    Please Login or Register  to view this content.

    This uses just one Copy / Paste instruction rather than the many you would if you're looping through many cells.
    In your original your variable 'first' was not being changed anyway. It retained its C2 value all through
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Loop problem

    @ Richard Buttrey,

    Why do you always through a spanner in the works, just to proof that you are correct?

    I agree with you my friend!

    Keep on going strong.

    Kind Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  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: Loop problem

    I think it is in the best interest of the student if the teacher can connect on the student's level of understanding. There will be plenty of time to revisit and improve as does the student's knowledge.

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Loop problem

    Quote Originally Posted by Richard Buttrey View Post
    Please Login or Register  to view this content.
    You are trying to grab everything after the comma, correct? To do that using the RIGHT function would require you to subtract the result of the FIND function from the length of the entire text string minus an additional 1 to adjust for the subtraction... or, alternately, you could use the MID function and specify a large enough number for the third argument. So either this...

    "=IFERROR(RIGHT(C2,LEN(C2)-FIND("","",C2)-1),"""")"

    or this...

    "=IFERROR(MID(C2,FIND("","",C2)+2,99),"""")"

    Note: The subtraction of 1 in the first formula and addition of 2 in the second formula assumes there is a space after the comma. If this is not the case, then remove the -1 from the first formula and change the +2 to +1 in the second formula.

    Now, with that said, we can remove the IFERROR function call entirely from the second formula by simply making sure the FIND function can always find a comma...

    "=MID(C2,FIND("","",C2&"","")+2,99)"

    And again, the +2 assumes there are spaces after the commas... change it to +1 if there will not be spaces after the commas.


    EDIT NOTE: Here is another way to write a non-looping macro to do what the OP asked for. This macro could possibly be faster as it eliminates placing formulas in Column D only to Copy/Paste the calculated values over top of those formulas...
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 08-18-2019 at 12:54 AM.

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

    Re: Loop problem

    To summarize.
    Please Login or Register  to view this content.
    The last one is an array method which in speed comes the closest to the formula ways.
    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.

  9. #9
    Registered User
    Join Date
    08-03-2019
    Location
    Illinois
    MS-Off Ver
    Mac 14.7.7
    Posts
    17

    Re: Loop problem

    I just wanted to say a big "THANK YOU' to all of you who posted here. This is so helpful for me to see the different ways of doing this. As you can tell I'm hacking things together from bits and pieces I'm learning but as you can tell my knowledge so far is limited. I'm really a web designer so very new to this but I find the VBA scripting really interesting. Thanks again. Have a great weekend all

  10. #10
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Loop problem

    Quote Originally Posted by bakerman2 View Post
    To summarize.
    Please Login or Register  to view this content.
    The last one is an array method which in speed comes the closest to the formula ways.
    Just out of curiosity, do you have any timing results for those three macros running on your computer?

  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: Loop problem

    @ Rick

    Tested on 10K rows

    getfirst = 0.14 sec

    getfirst2 = 0.15 sec

    Grabfirst = 0.18 sec

  12. #12
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Loop problem

    Quote Originally Posted by bakerman2 View Post
    @ Rick

    Tested on 10K rows

    getfirst = 0.14 sec

    getfirst2 = 0.15 sec

    Grabfirst = 0.18 sec
    Thanks for running the tests. Interestingly, the code you thought would be closest to the speed of a formula came in the slowest. HOWEVER, those timing results for 10K rows of data are close enough to each other that they can be considered equal. Given that, the OP cannot go wrong with whichever macro is used.

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

    Re: Loop problem

    I was under the impression that working in memory would always be the fastest but from previous tests I noticed that putting in formulas and then converting to values

    was always faster, no matter how large the testrange. That's why I wrote 'comes closest to' knowing that the formula way would always be slightly faster.

  14. #14
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Loop problem

    Quote Originally Posted by bakerman2 View Post
    ... but from previous tests I noticed that putting in formulas and then converting to values was always faster
    That surprises me as well. One would think forcing Excel to load a large number of formulas into its cells would be somewhat time consuming and then one would converting the formulas to values would require some additional time. As for you code compared to mine... I am surprised to find mine being faster... I knew it would be close, but I expected it to be a smidge slower than your code. Surprises all-around.

  15. #15
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Loop problem

    @ Leith Ross,

    Hi Leith, a very valid point, and I would even add to it as follow;

    I think it is in the best interest of the OP's level of understanding. There will be plenty of time to revisit and improve as does the OP's knowledge.
    Kind Regards.
    Last edited by Winon; 08-20-2019 at 12:06 PM. Reason: Responding to level of knowledge of VBA of OP

+ 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. Problem with Loop
    By josepaulsam in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-27-2016, 09:38 AM
  2. [SOLVED] Another loop problem
    By SIMBAtheCAT in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-18-2014, 12:21 PM
  3. [SOLVED] For Loop Problem
    By sperry2565 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-03-2013, 02:52 PM
  4. Vlookup problem in a loop with cell property and variable cell problem (long title sry)
    By ExcelsiorLux in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2013, 10:38 AM
  5. [SOLVED] Loop inside a loop problem!
    By questionguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-22-2012, 12:54 PM
  6. Problem with do..while..until..loop
    By BeerKing in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-05-2010, 04:51 AM
  7. Problem adding charts using Do-Loop Until loop
    By Chris Bromley in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-23-2005, 09:06 AM

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