if value x or y, delete until next value.

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,
I'm working with some data that looks like this...

CAP8503MKTG04EAOM
1
CAP8504MKTG01EAOW
35-381PO: 0009692
CAP8504MKTG03EAOW
27-51PO: 0009168
CAP8505MKTG01EAOP
59-10PO: 0008577
59-125PO: 0009692
59-125PO: 0009693
CAP8505MKTG04EAOP
1


Column B holds the values of 01, 03, or 04. I need to check each value in column B and delete any rows that are 03 or 04 until the next value in Column B. My end result should look like this...
CAP8504MKTG01EAOW
35-381PO: 0009692
CAP8505MKTG01EAOP
59-10PO: 0008577
59-125PO: 0009692
59-125PO: 0009693


I'd like to be able to add some vba to an automated routine that I'm building to do a few more things with the data but I'm stuck on this part. Any assistance is greatly appreciated.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
See if this works once you change the sheet name to the correct one. It assumes you start at row 1 and also that the first number is available in B1.

VBA Code:
Sub Macro1()

Dim lr As Long, arr, i As Long, x As String, rng As Range

With Sheets("Sheet1")
    lr = .Range("C" & .Rows.Count).End(xlUp).Row
    arr = .Range("B1:B" & lr)
    For i = LBound(arr, 1) To UBound(arr, 1)
        If Len(arr(i, 1)) = 0 Then
            arr(i, 1) = x
        Else
            x = arr(i, 1)
        End If
    Next
    For i = LBound(arr, 1) To UBound(arr, 1)
        If arr(i, 1) = "03" Or arr(i, 1) = "04" Then
            If rng Is Nothing Then
                Set rng = .Rows(i)
            Else
                Set rng = Union(rng, .Rows(i))
            End If
        End If
    Next
    If Not rng Is Nothing Then rng.Delete Shift:=xlUp
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,749
Members
449,050
Latest member
excelknuckles

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