+ Reply to Thread
Results 1 to 7 of 7

Simple Loop command

Hybrid View

chrismann85 Simple Loop command 03-24-2011, 06:31 AM
TMS Re: Simple Loop command 03-24-2011, 06:55 AM
abousetta Re: Simple Loop command 03-24-2011, 06:59 AM
abousetta Re: Simple Loop command 03-24-2011, 07:02 AM
chrismann85 Re: Simple Loop command 03-24-2011, 07:02 AM
abousetta Re: Simple Loop command 03-24-2011, 07:29 AM
TMS Re: Simple Loop command 03-24-2011, 08:26 AM
  1. #1
    Registered User
    Join Date
    01-07-2008
    Location
    Northampton, UK
    MS-Off Ver
    version 2212, office 365 enterprise
    Posts
    74

    Simple Loop command

    Hi All,

    Really simple question here. I'm trying to delete a row if the value in the Column 62 is 0.

    Here's what I have so far:

    Sub findlast300()
    Dim row As Integer
    row = 3
    For i = 1 To 9000
    If Cells(row, 62) = 0 Then
    
    Rows(row).Select
    Selection.Delete Shift:=x1Up
    Else: row = row + 1
    End If
    i = i + 1
    Next
    
    End Sub
    It runs ok and deletes the row but doesn't seem to recognise when the value in column 62 (BJ) is equal to 1.

    can anyone help?

    Thanks
    Ne auderis delere orbem rigidum meum!

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,517

    Re: Simple Loop command

    When deleting rows, always start from the bottom.

    Try this:

    Sub findlast300_v2()
    Dim lRC As Long ' row counter
    Application.ScreenUpdating = False
    For lRC = 9000 To 3 Step -1
        If Cells(lRC, 62) = 0 Then
            Rows(lRC).Delete Shift:=x1Up
        End If
    Next
    Application.ScreenUpdating = True
    End Sub

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Simple Loop command

    Hi,

    This seems to work for me, unless I don't understand the problem:

    Option Explicit
    
    Sub findlast300()
    Dim row As Integer
    Dim i As Integer
    
    row = 3
    For i = 1 To 9000
    If Cells(row, 62) = 0 Then
    
    Rows(row).Delete Shift:=xlUp
    Else: row = row + 1
    End If
    i = i + 1
    Next
    
    End Sub
    abousetta

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Simple Loop command

    Also you might want to limit the number of rows unless you always have 9000 rows of data. This seems to be slowing down the macro.

    In general, TMShucks approach is more efficient.

    abousetta

  5. #5
    Registered User
    Join Date
    01-07-2008
    Location
    Northampton, UK
    MS-Off Ver
    version 2212, office 365 enterprise
    Posts
    74

    Re: Simple Loop command

    Quote Originally Posted by abousetta View Post
    Hi,

    This seems to work for me, unless I don't understand the problem:

    abousetta
    Yep, tried it again and it seemed to work.

    I'll try to new code to delete from the bottom.

    Thanks for the help guys.

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Simple Loop command

    Also one small detail. Its xlUp instead of x1UP. That might be the source of the problem you were having.

    abousetta

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,517

    Re: Simple Loop command

    Starting from the top can "seem" to work unless you encounter several rows together that meet the criterion. Then you can leapfrog rows.

    Regards

+ 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