delete rows

I

Ivor Williams

I've a spreadsheet of inventory information that was exported from
QuickBooks. The resulting spreadsheet contains a number of rows with no
information in the column I want to sort on, so I would like to
automatically delete those rows. I'm totally new to writing VBA for Excel,
but I'm quite comfortable with it in Access, so understand it somewhat. I
also understand I can modify the QuickBooks report so the spreadsheet is
created in a format that will work, but I would like some assistance with
the code so I can start to learn what I'm doing in Excel.

Thanks,
Ivor
 
J

JLGWhiz

You will need to adjust to your file criteria.

Sub DelRw()
lstRw = Cells(Rows.Count, 1).End(xlUp).Row
For i = lstRw To 2 Step -1 'Assumes Header Row
If Cells(i, 1) = "" Then 'Change col 1 to actual
Cells(i, 1).EntireRow.Delete
End If
Next
End Sub
 
I

Ivor Williams

Thank you for the suggestion. Would you be kind enough to explain the code
so I understand what each line means.

Ivor
 
J

JLGWhiz

Sure:

Sub DelRw() 'This is the title line which you can use when
'calling this macro. Just type DelRw in other code
'within the same workbook.

lstRw = Cells(Rows.Count, 1).End(xlUp).Row 'This line sets the
'variable lstRw equal to the number of the last row
'containing data.


For i = lstRw To 2 Step -1 'Assumes Header Row and establishes a
'beginning for a loop with a
numerical limit.
'Since you are deleting entire rows,
it starts
'at the bottom so no rows will be
skipped due
' to the automatic upward shift
after delete.

If Cells(i, 1) = "" Then 'Change col 1 to actual and sets the
'criteria for deleting a
row. In this case
'it starts with the last row
with data in
'column A.

Cells(i, 1).EntireRow.Delete 'Executes the delete action for the cell
'if it met the criteria,
which in this case is
'if the cell = null string.


End If 'Closes the if block

Next 'Goes to nest item up on the active sheet and
'repeats the process until the For limits expire.

End Sub 'Closes the procedure.
 
R

RichardSchollar

Hi Ivor

If you have a lot of rows (ie into the 1000s) then using autofilter
will increase the spped of execution considerably over looping:

Sub RemoveBlanks()
Dim r As Range
Set r = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row) 'spec the
range to run from A1 to the last row
'note that presumed header in row 1. Change "A" column to the actual
column
With r
.AutoFilter field:=1, Criteria1:="=" 'filter range for blanks
.Offset(1).EntireRow.Delete 'offset so don't delete header row
(only visible cells will be deleted)
.AutoFilter 'turn off autofilter
End With
End Sub

Best regards

Richard
 
I

Ivor Williams

I've tried the following and get an Error 400. If I step into the code, it
gets hung up on the second line. G is the actual column I want to check for
null values.
Sub RemoveBlanks()

Dim r As Range
Set r = Range("A1:A" & Cells(Rows.Count, "G").End(x1Up).Row)
With r
.AutoFilter field:=1, Criteria1:="="
.Offset(1).EntireRow.Delete
.AutoFilter
End With

End Sub

Thanks,
Ivor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top