I have code that is not working, but it is close. This code is supposed to delete rows based on a value in column B. If the row does not have that value, delete it. Nothing happens when I run it, though if I change the "does not equal" function to "equals", it deletes the rows that have the referenced values. Why wont this code do the opposite when I put <> in place of =?
Dim arcArray As Variant
Dim d As Integer
arcArray = Array("Tanker Offer Data", "Truck Offer Data", "Pipeline Offer Data", "Barge & SDraft Offer Data", "Offeror Max Monthly")
Dim ws5 As Worksheet
For Each arcSheet In arcArray
sheets(arcSheet).Select
lastrow = sheets(arcSheet).Range("B" & Rows.Count).End(xlUp).Row
Range("B6:B" & lastrow).Select
For d = 6 To lastrow
If ActiveCell.Value <> ActiveSheet.Range("E1").Value Then
ActiveCell.EntireRow.Delete
End If
Next d
Next arcSheet
>Solution :
- Collect all desired range and delete it all at once
Option Explicit
Sub demo()
Dim arcArray As Variant, vCell, delRng As Range
Dim d As Long, oSht As Worksheet, c As Range
arcArray = Array("Tanker Offer Data", "Truck Offer Data", "Pipeline Offer Data", "Barge & SDraft Offer Data", "Offeror Max Monthly")
For Each arcSheet In arcArray
Set oSht = Sheets(arcSheet)
vCell = oSht.Range("E1").Value
lastrow = oSht.Range("B" & oSht.Rows.Count).End(xlUp).Row
Set delRng = Nothing
For d = 6 To lastrow
Set c = oSht.Cells(d, "B")
If c.Value <> vCell Then
If delRng Is Nothing Then
Set delRng = c
Else
Set delRng = Application.Union(delRng, c)
End If
End If
Next d
If Not delRng Is Nothing Then
delRng.EntireRow.Delete
End If
Next arcSheet
End Sub