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
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,530
Messages
6,114,162
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top