Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA - Delete empty rows

Tags:

excel

vba

I would like to delete the empty rows my ERP Quotation generates. I'm trying to go through the document (A1:Z50) and for each row where there is no data in the cells (A1-B1...Z1 = empty, A5-B5...Z5 = empty) I want to delete them.

I found this, but can't seem to configure it for me.

On Error Resume Next
Worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
like image 489
CustomX Avatar asked Feb 21 '12 14:02

CustomX


1 Answers

How about

sub foo()
  dim r As Range, rows As Long, i As Long
  Set r = ActiveSheet.Range("A1:Z50")
  rows = r.rows.Count
  For i = rows To 1 Step (-1)
    If WorksheetFunction.CountA(r.rows(i)) = 0 Then r.rows(i).Delete
  Next
End Sub
like image 131
Alex K. Avatar answered Sep 21 '22 16:09

Alex K.