Deleting Variable Amount of Rows

alexdelwar

New Member
Joined
May 4, 2018
Messages
3
Hello,

Would truly appreciate if anyone can help me with this problem.

Original data:


Any help would be appreciated.
 
Last edited by a moderator:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Sorry. Messed up attaching photos. First time posting something...

Hello,

Would truly appreciate if anyone can help me with this problem.

Original data:
X
1321
42145

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123412

<colgroup><col width="64"></colgroup><tbody>
</tbody>
Y
31231

<colgroup><col width="64"></colgroup><tbody>
</tbody>
4

<colgroup><col width="64"></colgroup><tbody>
</tbody>
32131

<colgroup><col width="64"></colgroup><tbody>
</tbody>
432

<colgroup><col width="64"></colgroup><tbody>
</tbody>
X

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123

<colgroup><col width="64"></colgroup><tbody>
</tbody>
Y

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123

<colgroup><col width="64"></colgroup><tbody>
</tbody>
X

<colgroup><col width="64"></colgroup><tbody>
</tbody>
4

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123

<colgroup><col width="64"></colgroup><tbody>
</tbody>
X

<colgroup><col width="64"></colgroup><tbody>
</tbody>
1

<colgroup><col width="64"></colgroup><tbody>
</tbody>
241

<colgroup><col width="64"></colgroup><tbody>
</tbody>
23

<colgroup><col width="64"></colgroup><tbody>
</tbody>
124

<colgroup><col width="64"></colgroup><tbody>
</tbody>

<tbody>
</tbody>

I need to delete rows with the letter Y in column A and any rows below until the next letter appears in column A. It doesn't matter what the actual next letter is. I'm looking for vba code as i have thousands of rows.

Result:

X
1321

<colgroup><col width="64"></colgroup><tbody>
</tbody>
42145

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123412

<colgroup><col width="64"></colgroup><tbody>
</tbody>
X

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123

<colgroup><col width="64"></colgroup><tbody>
</tbody>
X

<colgroup><col width="64"></colgroup><tbody>
</tbody>
4

<colgroup><col width="64"></colgroup><tbody>
</tbody>
123

<colgroup><col width="64"></colgroup><tbody>
</tbody>
X

<colgroup><col width="64"></colgroup><tbody>
</tbody>
1

<colgroup><col width="64"></colgroup><tbody>
</tbody>
241

<colgroup><col width="64"></colgroup><tbody>
</tbody>
23

<colgroup><col width="64"></colgroup><tbody>
</tbody>
124

<colgroup><col width="64"></colgroup><tbody>
</tbody>

<tbody>
</tbody>


Any help would be appreciated.
 
Upvote 0
Slow method...

Rich (BB code):
Sub DeleteY()
    Dim i As Long, lr As Long
    
    Application.ScreenUpdating = False
    
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
    For i = lr To 1 Step -1
        If UCase(Cells(i, "A")) = "Y" Then Range(Cells(i, "A"), Cells(i, "A").End(xlDown).Offset(-1)).EntireRow.Delete
    Next
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Fast method, though you may not notice the difference unless you have tens or hundreds of thousands of rows.

Code:
Sub Del_Y_and_Following_Blanks()
  Dim a, b
  Dim nc As Long, i As Long, k As Long

  nc = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlValues, SearchOrder:=xlByColumns, _
                SearchDirection:=xlPrevious, SearchFormat:=False).Column + 1
  a = Range("A1", Range("B" & Rows.Count).End(xlUp).Offset(1)).Value
  a(UBound(a), 1) = "@"
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) = "Y" Then
      b(i, 1) = 1
      k = k + 1
      Do Until a(i + 1, 1) <> ""
        i = i + 1
        k = k + 1
        b(i, 1) = 1
      Loop
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A1").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Slow method...
Mark, I don't know if it is possible with the OP's data, but if the following was possible, your code would delete rows that it shouldn't.


Book1
AB
1Y1
21
3Y1
4X1
5X1
6X1
7X1
Sheet2
 
Upvote 0
Mark, I don't know if it is possible with the OP's data, but if the following was possible, your code would delete rows that it shouldn't.

I know but I based my reply on the data posted :biggrin:
 
Upvote 0
I know but I based my reply on the data posted :biggrin:
Fair enough. I interpreted the any in the OP's comments to mean the possibility of none. At least the issue has been raised for the OP or other readers. :)

.. delete rows with the letter Y in column A and any rows below until the next letter appears in column A.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

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