VBA Question

madab

New Member
Joined
Jan 29, 2014
Messages
27
Hi - hoping someone might be able to help with this - VBA skills not the best... I have a number of rows that I would like to delete from a worksheet. The rows will be concurrent but the number of rows that needs to be deleted will be dependant upon the values in two cells. For example: if B29 = "x" and B51 = "y" then delete between rows 29 and 51. Hope the above makes sense - any help would be greatly appreciated! Thanks
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

madab

New Member
Joined
Jan 29, 2014
Messages
27
Yes if I understand you correctly. What I need to happen is that the macro will find "X" in column B and then continue down column but until it finds "Y" in column B, then delete the rows in between. Then repeat this again if it finds "X" and "Y" in column B again but do nothing if it doesn't. If it doesn't find them in the first instance, do nothing or carry on to next step in macro.

Dos that make sense?
 

NeonRedSharpie

Well-known Member
Joined
Jul 14, 2014
Messages
1,678
Code:
Sub deleteRowsXY()    Dim deletion As Boolean
    deletion = False


    For x = Cells(Rows.Count, "B").End(xlUp).Row To 2 Step -1
        Select Case deletion
            Case False
                If UCase(Cells(x, 2)) = "Y" Then
                    deletion = True
                End If
            Case True
                If UCase(Cells(x, 2)) <> "X" Then
                    Cells(x, 2).EntireRow.Delete
                Else
                    deletion = False
                End If
        End Select
    Next x
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,108,816
Messages
5,525,064
Members
409,617
Latest member
Lenaf

This Week's Hot Topics

Top