VBA Delete row if certain cell ifs blank

ORoxo

Board Regular
Joined
Oct 30, 2016
Messages
149
Hey, everyone,
I need your help once again.

I have lots of data and would need to delete rows if certain cells are blank (For instance, if column X, Z or Y in a row are blank, I want the macro to delete the whole row)

Typically I would try to do it myself but I have a deadline and time is running out. Do you think you could help me out?

Kind regards,
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
this works?

Code:
Sub Oroxo()
For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
    If Cells(i, 24) = "" Or Cells(i, 25) = "" Or Cells(i, 26) = "" Then Cells(i, 1).EntireRow.Delete xlShiftUp
Next
End Sub
 
Upvote 0
Assuming there will always be data in column "A"
Try this

Code:
Sub Test_New()
'Modified 3-4-18 6:40 AM EST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Lastrow To 1 Step -1
        If Cells(i, "x").Value = "" Or Cells(i, "Y").Value = "" Or Cells(i, "Z").Value = "" Then Rows(i).Delete
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I have lots of data ...
Depends just how much you mean by "lots", but this should process a large number of rows very quickly.

Code:
Sub Del_Rw_Blanks_XYZ()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, j As Long, k As Long, uba2 As Long
 
  nc = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, SearchFormat:=False).Column + 1
  a = Intersect(ActiveSheet.UsedRange, Columns("X:Z")).Value
  uba2 = UBound(a, 2)
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 2 To UBound(a)
    For j = 1 To uba2
      If IsEmpty(a(i, j)) Then
        b(i, 1) = 1
        k = k + 1
        Exit For
      End If
    Next j
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A1").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlYes
      .Offset(1).Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Oroxo,

If I understand you correctly, then, here is another macro code that will run in the active worksheet.

Please test the following macro on a copy of your workbook/worksheet.


Code:
Sub Oroxo_V2()
' hiker95, 03/04/2018, ME1046020
Application.ScreenUpdating = False
Dim r As Long, lr As Long
With ActiveSheet
  lr = .Columns("X:Z").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
  For r = lr To 2 Step -1
    If .Cells(r, "X") = vbEmpty Or .Cells(r, "Y") = vbEmpty Or .Cells(r, "Z") = vbEmpty Then
      .Rows(r).Delete
    End If
  Next r
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this:
Code:
Sub Filter_Me()
'Modified 3-4-2018 7:25 AM EST
    With ActiveSheet.Range("X1:Z" & Cells(Rows.Count, "A").End(xlUp).Row)
        .AutoFilter 1, Criteria1:=""
        .Offset(1).SpecialCells(12).EntireRow.Delete
        .AutoFilter
        .AutoFilter 2, Criteria1:=""
        .Offset(1).SpecialCells(12).EntireRow.Delete
        .AutoFilter
        .AutoFilter 3, Criteria1:=""
        .Offset(1).SpecialCells(12).EntireRow.Delete
        .AutoFilter
    End With
End Sub
 
Upvote 0
@ hiker95
Your determination of lr is problematic. For example, your code does not delete the last two rows of this data, even though they meet the criteria for deletion.


Book1
WXYZAA
999516744
999644538
9997537
99985334
999932
1000047
Del Rws (2)




@ ORoxo
For consideration if your data is large. Testing the codes suggested on 10,000 rows of data, about 3,000 of which require deletion:

Post # 2: 11.938 seconds
Post # 4: 7.109 secs
Post # 5: 0.078 secs
Post # 6: 6.719 secs
Post # 7: 4.641 secs
 
Last edited:
Upvote 0
@ hiker95
Your determination of lr is problematic. For example, your code does not delete the last two rows of this data, even though they meet the criteria for deletion.

WXYZAA

<colgroup><col style="width: 25pxpx"><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]9995[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]4[/TD]

[TD="align: center"]9996[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]8[/TD]

[TD="align: center"]9997[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]7[/TD]

[TD="align: center"]9998[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"]4[/TD]

[TD="align: center"]9999[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]

[TD="align: center"]10000[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]7[/TD]

</tbody>
Del Rws (2)





@ ORoxo
For consideration if your data is large. Testing the codes suggested on 10,000 rows of data, about 3,000 of which require deletion:

Post # 2: 11.938 seconds
Post # 4: 7.109 secs
Post # 5: 0.078 secs
Post # 6: 6.719 secs
Post # 7: 4.641 secs

Peter:
I hear people on this forum saying don't use loops they are too slow.
So I use filter but your saying your script which uses a loop is faster then a filter. Why is that?
 
Upvote 0
Oroxo,

Here is an updated macro code that will run in the active worksheet.

Please test the following macro on a copy of your workbook/worksheet.


Code:
Sub Oroxo_V3()
' hiker95, 03/04/2018, ME1046020
Application.ScreenUpdating = False
Dim r As Long, lr As Long
With ActiveSheet
  lr = .Columns("A:Z").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
  For r = lr To 2 Step -1
    If .Cells(r, "X") = vbEmpty Or .Cells(r, "Y") = vbEmpty Or .Cells(r, "Z") = vbEmpty Then
      .Rows(r).Delete
    End If
  Next r
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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