VBA delete empty rows that start with a determined text

mickeystanford_alumni

Board Regular
Joined
May 11, 2022
Messages
123
Office Version
  1. 2021
Platform
  1. Windows
  2. MacOS
Hi all,

I hope you are good.
I am trying to do the following: After having a database with thousands of rows, some of them appear as empty. Also, 99% of them follow a determined format, but there are a few which look totally different. Therefore, I would like to do the following:
1. All empty rows, delete (shift up).
2. The rows that start with ALL_COUNTRY in column A, delete entire row.

Thank you so much.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Good day....

Try the following:

1.

VBA Code:
Private Sub CommandButton1_Click()
    Columns("A:A").Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.EntireRow.Delete
End Sub

2.

VBA Code:
Private Sub CommandButton2_Click()
    With ActiveSheet
        .AutoFilterMode = False
        With .Range("A1", .Range("A" & Rows.Count).End(xlUp))
            .AutoFilter 1, "*" & InputBox("Search for?") & "*"
            On Error Resume Next
            .Offset(1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With
End Sub
 
Upvote 0
some of them appear as empty.
That is a bit worrying as cells that 'appear' empty can contain space characters or other non-visible characters.

However, assuming that the cells are empty or contain zero length data, if your data is large, this should be a very fast way to delete the rows.

VBA Code:
Sub Del_Blank_And_AllCountry()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If Len(a(i, 1)) = 0 Or a(i, 1) Like "ALL_COUNTRY*" Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A2").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub

If neither of the above suggestions do what you want, please give us some small dummy sample data with XL2BB so the we can test with that.
 
Upvote 0
Solution
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

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