VBA to Delete Rows based on Specific Text appearing in a column

dids86

New Member
Joined
Aug 15, 2024
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm using an excel workbook with multiple sheets but within the sheet called REP500, I'd like to run a macro that deletes all rows where the value isn't "NNN" in column AF

Would anyone be able to suggest a suitable macro for this please? It's likely to need to run down to 10,000 rows.

Thanks

Tom
 
You're welcome. :)

Did you get a noticeable speed improvement with your own data?
I've got to say it is unreal! So fantastic thank you. Which leads me to something else. Can a similar macro be utilised for hiding rows instead of deleting them with the same conditions applied?
 
Upvote 0
I've got to say it is unreal! So fantastic thank you.
Cheers. Yes, much faster than row-by-row deletion. (y)

Can a similar macro be utilised for hiding rows instead of deleting them with the same conditions applied?
Yes

VBA Code:
Sub HideNotNNN()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long

  With Sheets("REP500")
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("AF20", .Range("AF" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) <> "NNN" Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Cells(20, nc).Resize(UBound(a))
        .Value = b
        .SpecialCells(xlConstants).EntireRow.Hidden = True
        .ClearContents
      End With
      Application.ScreenUpdating = True
    End If
  End With
End Sub
 
Upvote 0
Cheers. Yes, much faster than row-by-row deletion. (y)


Yes

VBA Code:
Sub HideNotNNN()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long

  With Sheets("REP500")
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("AF20", .Range("AF" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) <> "NNN" Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Cells(20, nc).Resize(UBound(a))
        .Value = b
        .SpecialCells(xlConstants).EntireRow.Hidden = True
        .ClearContents
      End With
      Application.ScreenUpdating = True
    End If
  End With
End Sub
Thanks Peter
 
Upvote 0

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