+ Reply to Thread
Results 1 to 9 of 9

Do While loop not breaking?

  1. #1
    Forum Contributor
    Join Date
    09-05-2019
    Location
    Vancouver, Canada
    MS-Off Ver
    Office 365
    Posts
    101

    Do While loop not breaking?

    Hello

    Perhaps there is a better way to write my intentions, but I decided to write a Do...While loop in order to delete all the Steps (in rows) after #1. However, it is deleting more than it should, but it shouldn't be that way. Since I'm always checking whether the .Value <> "", it should immediately break if the value of the particular cell that I'm looking for is blank.

    However, the loop continues to delete one more row after that, so I'm confused why the conditional is not acting correctly. Any help in this would be greatly appreciated.

    The worksheet labeled "System_Template_Intention" is my intended action and the "System_Template_Result" is what I get after running the macro "DefaultReset"

    Thanks in advance for your help
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,194

    Re: Do While loop not breaking?

    It's not the code that's wrong, it's the fact that A8 contains as space & therefore is not ""

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Do While loop not breaking?

    This will do what you want:

    Please Login or Register  to view this content.
    I do not recommend repetitive deletions on a worksheet when it can be done all at once at the end.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Contributor
    Join Date
    09-05-2019
    Location
    Vancouver, Canada
    MS-Off Ver
    Office 365
    Posts
    101

    Re: Do While loop not breaking?

    Quote Originally Posted by JBeaucaire View Post
    This will do what you want:

    Please Login or Register  to view this content.
    I do not recommend repetitive deletions on a worksheet when it can be done all at once at the end.
    Does your code work if the first column contains things other than numbers. There are some scenarios I can think of in which the values are strings as well.

  5. #5
    Forum Contributor
    Join Date
    09-05-2019
    Location
    Vancouver, Canada
    MS-Off Ver
    Office 365
    Posts
    101

    Re: Do While loop not breaking?

    @JBeaucaire

    I copied your code into my sample workbook and only took out the the
    Please Login or Register  to view this content.
    within the Do Loop because I wanted wanted to do other stuff after I finished deleting the rows.

    However, this resulted in a Runtime error 424: Object Required. It succesfully deletes the intended rows, but cannot execute the rest of the code (clearing the contents in Sequence #1).

    See my attahment.
    Attached Files Attached Files

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Do While loop not breaking?

    You would need to change the EXIT SUB line in the original code to EXIT DO.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    Quote Originally Posted by dcwan View Post
    Perhaps there is a better way to write my intentions
    According to your attachment a VBA 'better way' :

    PHP Code: 
    Sub DefaultReset()
        
    With [A3].CurrentRegion
            
    .Rows(2).Offset(, 1).ClearContents
            
    .Columns(1) = Application.Trim(.Columns(1))
        
    End With
            L
    & = [A3].CurrentRegion.Rows.Count 2
            
    If 0 Then Rows(5).Resize(L).Delete
        With ActiveSheet
    .UsedRange.Rows
            
    .Item(.Count).ClearContents
        End With
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 03-30-2020 at 10:51 PM. Reason: adding ClearContents codelines …

  8. #8
    Forum Contributor
    Join Date
    09-05-2019
    Location
    Vancouver, Canada
    MS-Off Ver
    Office 365
    Posts
    101

    Re: Do While loop not breaking?

    Thank you everyone for your help.

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,194

    Re: Do While loop not breaking?

    You're welcome & thanks for the feedback

+ 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] Auto email loop with formatted table data breaking on second loop VBA
    By Who_else in forum Excel General
    Replies: 6
    Last Post: 03-24-2016, 06:22 AM
  2. Breaking loop if condition met
    By kaderk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-30-2015, 11:09 AM
  3. [SOLVED] Find function in nested loop breaking down - not on first time through loop
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-14-2014, 09:32 AM
  4. Find function in nested loop breaking down - not on first time through loop
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2014, 04:59 PM
  5. LOOP formula breaking
    By donyc in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-08-2012, 04:28 PM
  6. Breaking out of a loop
    By hojkoff in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-29-2010, 07:29 AM
  7. Breaking a Find Loop
    By Jmoo in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 06-23-2010, 10:55 AM

Tags for this Thread

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