Dynamic Delete

  • Thread starter Thread starter Dayton
  • Start date Start date
D

Dayton

I use the below marco to delete rows of unwanted data. This works exactly
how I needed it to. However the values are dynamic from day to day and are
increasing from 3 different values to 20 or more. Insted of typing new lines
of AND statements each day is there a way to reference these values.

Thanks
Dayton


Sub Delete()

Dim LastRow As Long
Dim x As Long
LastRow = Range("A6536").End(xlUp).Row

For x = LastRow To 1 Step -1
If Not (Range("A" & x).Value Like "FZA3764R") _
And Not (Range("B" & x).Value Like "JGA9799T") _
And Not (Range("B" & x).Value Like "RFZ3763B") _
Then
Range("A" & x).EntireRow.Delete
End If
Next 'x
End Sub
 
Dayton,

Try setting an array of your values, and then looping through the arrays.
See the sample code below.

HTH,
Bernie
MS Excel MVP

Sub Delete()

Dim LastRow As Long
Dim x As Long
Dim i As Integer
Dim myAVals As Variant
Dim myBVals As Variant

myAVals = Array("FZA3764R")
myBVals = Array("JGA9799T", "RFZ3763B")

LastRow = Range("A6536").End(xlUp).Row

For x = LastRow To 1 Step -1
For i = LBound(myAVals) To UBound(myAVals)
If Range("A" & x).Value = myAVals(i) Then GoTo Skip:
Next i
For i = LBound(myBVals) To UBound(myBVals)
If Range("B" & x).Value = myBVals(i) Then GoTo Skip:
Next i
Range("A" & x).EntireRow.Delete
Skip:
Next x
End Sub
 
A slight typo in the original note methinks, as I'm guessing that
LastRow = Range("A6536").End(xlUp).Row

should be

LastRow = Range("A65536").End(xlUp).Row
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top