+ Reply to Thread
Results 1 to 32 of 32

Do until loop that doesn't seem to end

  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Do until loop that doesn't seem to end

    It worked once and found the forth valid cell in the range, ever since then it starts and then excel stops responding.

    I'm guessing there is no end to the loop but not sure,can someone tell me whats up with this code please?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Do until loop that doesn't seem to end

    I fail to see the need for your Do/Loop. You are using a For/Next loop. Could you just add a line checking if IsEmpty(CellRef(x).Value) = True then Exit For?

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Do until loop that doesn't seem to end

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    Quote Originally Posted by stnkynts View Post
    I fail to see the need for your Do/Loop. You are using a For/Next loop. Could you just add a line checking if IsEmpty(CellRef(x).Value) = True then Exit For?
    Isn't that what the below code line is doing?
    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Do until loop that doesn't seem to end

    You will have an infinite loop if this line is true

    Please Login or Register  to view this content.
    For that not to happen CellRef(x).Value at some point in the loop need to be empty

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Do until loop that doesn't seem to end

    Perhaps you need something other than IsEmpty to check the cell(s).
    If posting code please use code tags, see here.

  7. #7
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    Quote Originally Posted by AB33 View Post
    Please Login or Register  to view this content.
    If I understand correctly then yes the first Cellref0 will have a value and if I understood the code correct it will keep cycling through cellref2 2*4, cellref3 3*4 etc until it finds an empty cell, then it will stop, is this not correct?

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Do until loop that doesn't seem to end

    Please Login or Register  to view this content.
    will loop 11 times and for every loop CellRef(x) is set, but the last one would be loop 10 which is not zero and S2LIbe .Cells(RowNumba, 53 + (x * 4)) will not be unlikely to be zero.

    Under what condition CellRef(x) is empty or zero?

  9. #9
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    Please Login or Register  to view this content.
    and so on and so forth when one of these values is empty I want the loop to stop hence the below code, this is when condition CellRef(x) will be empty
    Please Login or Register  to view this content.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Do until loop that doesn't seem to end

    What's in the cells?

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Do until loop that doesn't seem to end

    zero and empty are not the same. your data may return zero, but not empty.
    You may try this line instead.

    Please Login or Register  to view this content.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Do until loop that doesn't seem to end

    AB33

    0 and "" aren't the same either.

  13. #13
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    saved document locations (i.e. web address)

  14. #14
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    Quote Originally Posted by AB33 View Post
    Please Login or Register  to view this content.
    That's the same as IsEmpty isn't it?

  15. #15
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    CellRef(x) value will be blank or text, the cell value is not number based

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Do until loop that doesn't seem to end

    "" is not the same as Empty, if you had a formula returning "" in the cell then the cell isn't empty.

  17. #17
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    There are no formulas creating anything in these cells.

    They are blank until another piece of code inputs a value into the first (horizontal) empty cell. Until then they are empty

  18. #18
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Do until loop that doesn't seem to end

    Scottle,
    We can not see what are in your data, but can only speculate and guess.
    Empty cell means there is no data in cell (a formula which returns a null string as Norie explained IS NOT EMPTY).
    So, my guess is I do not think the cell you are testing will be empty, but it may be blank.

  19. #19
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    cells are either going to be empty of look like this C:\Users\Scott\Desktop\Barclays Questions.doc

  20. #20
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Do until loop that doesn't seem to end

    Are you sure the Do loop is the problem?

  21. #21
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    I think so. It is constructed as follows, I hope the logic is sound.

    I know the SearchString, DocGrid, S2LI, Rownumba all work as I use them in other coding.

    The only coding on this I don't fully comprehend is the loop.

    the 1st for next sets the cells I want to check (rownumba starting at 53 and then moving every 4 cells)
    Please Login or Register  to view this content.
    This is basically a repetition of above set for the loop
    Please Login or Register  to view this content.
    This is the first line I want the grid to be produced on
    Please Login or Register  to view this content.
    The DoUntil users the x = 0 to 10 to cycle through the CellRef(x) - (Should this maybe "CellRef" & x ?)
    Then with DocGrid the border formatting and cell merging is created and the values for CellRef(x) and it's offsets are placed into the relevant cells in Sheet1 (DocGrid)
    Please Login or Register  to view this content.
    Then this increases the i by one for each positive result in the CellRef search and then loops for the next
    Please Login or Register  to view this content.
    Then this increases the CellRef x for the next search criteria
    Please Login or Register  to view this content.
    Last edited by Sc0tt1e; 03-31-2014 at 01:28 PM.

  22. #22
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,854

    Re: Do until loop that doesn't seem to end

    One of the debugging skills I use most is the ability to step through the code one line at a time http://office.microsoft.com/en-us/su...819.aspx?CTT=1 If you have not yet tried setting a breakpoint in this loop and monitoring variables as you execute the loop, I would strongly suggest that you try it. (sometimes I wonder if debugging skills are more important than actually writing the code).

    Stepping through the loop I see:
    x=0
    i=12
    cellref(0) is not empty, so execute the loop
    i=12+1=13
    go back to start of loop
    x=0 (still) so cellref(0) is still not empty, so execute the loop
    and so on.

    x never changes inside the Do until loop, so cellref(x) is never empty.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  23. #23
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    Yeah, I haven't reached the debugging chapter of the book yet lol

    So how do I change the x in the CellRef(x) within the Do until loop in line with the 0 To 10?

    i=12+1=13
    go back to start of loop

    These 2 need to be the other way round (i think)
    loop starts on i = 12 (line) 12
    loop
    next one (i = 12 + 1 = (line) 13)
    loop
    next one (i = 13 + 1 = (line) 14)
    Last edited by Sc0tt1e; 03-31-2014 at 02:56 PM.

  24. #24
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,854

    Re: Do until loop that doesn't seem to end

    So how do I change the x in the CellRef(x) within the Do until loop in line with the 0 To 10?
    If the compiler even allows you to modify the For..Next counter variable, I think it would be considered bad practice to modify x inside the loop. Beyond that, I'm not sure what to recommend. I would suggest you think through the underlying logic behind what you are trying to do here. Perhaps your real intention is to have the For x..Next x loop inside of the Do Until..Loop? Or perhaps the two loops are supposed to be combined so they are really one, single loop rather than two nested loops?

    Another of those programming skills I remember learning was putting together a flow chart. If you don't have a flow chart overview of what you want these loops to accomplish, it might be worth the time to pull out a sheet of paper and think through what this loop is supposed to do.

  25. #25
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    My flow is as follows

    SearchString value, finds Rownumba

    Search Rownumba and cell 56
    is it empty?

    if not paste the value and subsequent offset cells into row 12 with the cells merged, formatted etc (as per applymyformat)

    then search the next cell value (rownumber & 60)

    if not empty then do the same but move the pasted values to row 13

    then search the next cell value (rownumber & 64)

    if not empty then do the same but move the pasted values to row 14

    keep doing this until the cell value is blank or up to the maximum of 11 loops is reached. This stops the loop

  26. #26
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,854

    Re: Do until loop that doesn't seem to end

    This description sounds to me like one single loop rather than two nested loops. Granted there are two counter variables (one to count through the "input" range, and one to count through the "output" range), but still one single loop. In a kind of pseudocode I see it as:
    Please Login or Register  to view this content.
    or something like that.
    Last edited by MrShorty; 03-31-2014 at 04:41 PM.

  27. #27
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    Yes that's exactly what I'm looking for.

    I have played with the code some more. It is now cycling through the x count and copy pasting 1-4 (as these currently have values and then stops the loop), but it isn't increasing the i count, it keeps pasting in line 1 instead of creating 4 lines.

    I just can't work out where to place the i = i + 1 ?

    Please Login or Register  to view this content.

  28. #28
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,854

    Re: Do until loop that doesn't seem to end

    I think you'd be able to catch this one if you stepped through the loop as I suggested earler. I would expect you to see within the 2nd or third time through that you are resetting i to 12 at the beginning of each loop.

  29. #29
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    I was, that's how I worked out the placement of the x.
    I have placed i = i + 1 in all the places I can think of but had no success.

    2 things I have learnt from this, it cycles through all 10 x and only stops once it has cycled 10 times, the If Not only ends the call to ApplyMyFormat but still cycles through the 0-10.

    The i never increases so even though it finds x1, x2, x3, and x4 and pastes the values into the boxes where it should it just overlays the info on line 12 instead of increasing new lines to paste the information into.

  30. #30
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,854

    Re: Do until loop that doesn't seem to end

    I was hoping you would see that it isn't so much about the placement of the i=i+1 statement as it is the i=12 statement. Follow the process:

    x=0
    i=12
    copy and format
    i=i+1=13
    increment x (1) and return to i=12 statement
    i=12 (again)
    copy and format
    i=i+1
    and so on.

    The problem is the placement of the i=12 statement -- this statement needs to be outside of the loop (before the For x= statement).

  31. #31
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,624

    Re: Do until loop that doesn't seem to end

    Replace i = 12 ---> i = 12 + x
    Ben Van Johnson

  32. #32
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Do until loop that doesn't seem to end

    To MrShorty

    Whilst you posted that I just worked it out lol

    I was putting the i = 12 within the loop so everytime I was setting it as row 12

    Please Login or Register  to view this content.

+ 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] For Each loop doesn't take every row into account
    By Tino XXL in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-15-2012, 07:54 AM
  2. UDF Doesn't work with top level Do While-Loop
    By indium in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-02-2012, 01:17 PM
  3. Macro Loop Broken. Detects Max but doesn't continue loop
    By herchenbach in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2011, 12:17 PM
  4. My loop doesn't work why not
    By ljh66 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-06-2007, 01:02 PM
  5. [SOLVED] For Each - loop doesn't work.
    By Intellihome in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2005, 07:05 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