+ Reply to Thread
Results 1 to 6 of 6

Loop to Delete Rows and Shift Up on Cell Value

Hybrid View

  1. #1
    Registered User
    Join Date
    05-29-2013
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    37

    Loop to Delete Rows and Shift Up on Cell Value

    Hi,

    I'm trying to figure out a way to loop through rows H23 to AI23 all the way down and deleting any rows which don't contain the value "Yes" in the same row on column G.
    I also want to shift the bottom row up.

    In simple terms, I want the code to check whether the row contains "Yes" in column G and if it doesn't, then delete it and move the bottom row up into its position, but if it does, move on to the next row and repeat the same process until Activecell.Value = "".

    I just started learning macros a couple days ago so i apologize if this question is a bit elementary (i'm not very experienced with loops)

  2. #2
    Valued Forum Contributor
    Join Date
    11-20-2012
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2010
    Posts
    597

    Re: Loop to Delete Rows and Shift Up on Cell Value

    i use this alot...
    http://www.rondebruin.nl/win/s4/win001.htm

    anytime i'm doing any loop, i reference this first...

  3. #3
    Valued Forum Contributor
    Join Date
    11-20-2012
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2010
    Posts
    597

    Re: Loop to Delete Rows and Shift Up on Cell Value

    i use this alot...
    http://www.rondebruin.nl/win/s4/win001.htm

    anytime i'm doing any loop, i reference this first...

    sorry i don't get the part about moving up the bottom row part

  4. #4
    Valued Forum Contributor
    Join Date
    11-20-2012
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2010
    Posts
    597

    Re: Loop to Delete Rows and Shift Up on Cell Value

    ps h23:ai23 are in the same row??

  5. #5
    Valued Forum Contributor
    Join Date
    11-20-2012
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2010
    Posts
    597

    Re: Loop to Delete Rows and Shift Up on Cell Value

    Sub Loop_Example()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
    
        'Sheets("MySheet")if you want
        With ActiveSheet
    
            'Set the first and last row to loop through
            Firstrow = 23 '<--start row
            Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row '<--last used row in your workbook, could be any integer like Firstrow
    
            'We loop from Lastrow to Firstrow (bottom to top)
            For Lrow = Lastrow To Firstrow Step -1
    
                'We check the values in the A column in this example
                With .Cells(Lrow, "G")
    
                    If Not IsError(.Value) Then
    
                        If .Value <> "Yes" Then .EntireRow.Delete
                        'This will delete each row with the Value not equal to "Yes"
                        'in Column A, case sensitive.
    
                    End If
    
                End With
    
            Next Lrow
    
        End With
    
    End Sub

  6. #6
    Registered User
    Join Date
    05-29-2013
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Loop to Delete Rows and Shift Up on Cell Value

    Hey Scott,

    Thanks a lot for the help. Your code gave me the gist of what I wanted to do. I tweaked it slightly to fit my needs (since I wasn't able to explain myself in my OP). Btw, that's a great website!

    -Lauren

+ 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