Which is Fast way to delete Blank and Zero values, records entire Rows?

Guna13

Board Regular
Joined
Nov 22, 2019
Messages
70
Office Version
  1. 365
Platform
  1. Windows
Hi Team,

In a specific sheet I have over 10000 records, and I need to delete "Blank" data in the "B" column, then delete entire rows the same way I delete "0" values.

Can you suggest any other method or VBA code to delete fast for this code? It takes a little bit of time to delete.


VBA Code:
Trg.Columns("B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Trg.AutoFilterMode = False

Trg.Range("A1:B1000").AutoFilter Field:=2, Criteria1:="0"
  
  '2. Delete Rows
    Trg.Range("B2:B1000").SpecialCells(xlCellTypeVisible).Delete
  
  
  '3. Clear Filter
  On Error Resume Next
    Trg.AutoFilterMode = False
  On Error GoTo 0
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
For Blanks, Select the column, Press F5, click Special, select "Blanks", click OK, delete rows.
 
Upvote 0
Hi Awoohaw, I am need macro Code instead of Manual Delete Option, because this is one of the steps in my current macro project. Here I had stuck to get delay few minutes. Kindly advice.
 
Upvote 0
Can you suggest any other method or VBA code to delete fast
Yes, try this. It does assume that column B can be used to determine the last row, which may not be correct since that could be blank. A slight tweak might be needed.
However, you should find this very fast.
Test with a copy of your data though in case something does go wrong.

VBA Code:
Sub Del_Blank_and_0()
  Dim Trg As Worksheet
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
  
  Set Trg = ActiveSheet '<- Perhaps you have a worksheet name instead?
  With Trg
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("B2", .Range("B" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) = "" Or a(i, 1) = 0 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 With
End Sub
 
Upvote 0
Yes, try this. It does assume that column B can be used to determine the last row, which may not be correct since that could be blank. A slight tweak might be needed.
However, you should find this very fast.
Test with a copy of your data though in case something does go wrong.

VBA Code:
Sub Del_Blank_and_0()
  Dim Trg As Worksheet
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  Set Trg = ActiveSheet '<- Perhaps you have a worksheet name instead?
  With Trg
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("B2", .Range("B" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) = "" Or a(i, 1) = 0 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 With
End Sub
Great Thansk for your support
 
Upvote 0
Hi Awoohaw, I am need macro Code instead of Manual Delete Option, because this is one of the steps in my current macro project. Here I had stuck to get delay few minutes. Kindly advice.
Sub deleterowswithblankcellsincolumnB()
'
' deleterowswithblankcellsincolumnB Macro
'

'
Columns("B:B").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
End Sub
 
Upvote 0
Sub deleterowswithblankcellsincolumnB()
'
' deleterowswithblankcellsincolumnB Macro
'

'
Columns("B:B").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
End Sub
Yes i tried this method, but little bit slow, not fast delete to more than 5000 blank rows..
 
Upvote 0
@Peter_SSs why this code does not support to delete "C" Column I have #/N/A lines. where I need to modify to this code. Appreciate your advance support

VBA Code:
Sub Not_appl_Data_Delete()
  Dim Trg As Worksheet
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  Set Trg = ActiveSheet '<- Perhaps you have a worksheet name instead?
  With Trg
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("C2", .Range("C" & Rows.Count).End(xlUp)).Value
    ReDim C(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) = "#N/A" 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 With
End Sub

1674540271255.png
 
Upvote 0
Not the cause of the error you reported, but would have subsequently caused a problem is that you have changed this from a 'b' in my code.

1674550757584.png


Try this to delete #N/A rows.

VBA Code:
Sub Not_appl_Data_Delete()
  Dim Trg As Worksheet
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  Set Trg = ActiveSheet '<- Perhaps you have a worksheet name instead?
  With Trg
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("C2", .Range("C" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If IsError(a(i, 1)) Then
        If a(i, 1) = CVErr(xlErrNA) Then
          b(i, 1) = 1
          k = k + 1
        End If
      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 With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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