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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,826
Messages
5,855,875
Members
431,771
Latest member
CoryMelth

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
Top