Convert "Delete Entire Rows" VBA Code from Excel 2003 to 2010

TTTon1

New Member
Joined
Dec 27, 2018
Messages
4
Hello, ive been using the following code to delete entirely empty rows (only if all the cells in 1 row are empty) within a range in excel 2003.



Code:
Sub delete1()
  Dim r As Range, rows As Long, i As Long
  Set r = ActiveSheet.Range("A1:D220")
  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

This code doesnt work in Excel 2010.

Ive read many Threads regarding deleting entire Rows but found nothing that works for what I want to do.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
In what way does it not work? The code is the same in 2010.
 
Upvote 0
It doesnt work in the sense that it does nothing meaning it doesnt delete the empty rows within the set range. There are no errors.
 
Upvote 0
The only way that would happen is if there is at least one non-empty cell in each of the rows. That code will delete completely empty rows in 2010.
 
Upvote 0
I have Excel 2010 and had to make one minor modification to your code to get it to work (see the word in red):
Code:
Sub delete1()
  Dim r As Range, rows As Long, i As Long
  Set r = ActiveSheet.Range("A1:D220")
  rows = r.rows.Count
  For i = rows To 1 Step (-1)
    If WorksheetFunction.CountA(r.rows(i)) = 0 Then r.rows(i).[B][COLOR=#ff0000]EntireRow.[/COLOR][/B]Delete
  Next
  End Sub
But I guess it begs the question, are you checking for the WHOLE row to be blank, or just the row between columns A and D (your range)?
If the whole row, then what is the point of setting the range variable from columns A:D?
 
Last edited:
Upvote 0
That would delete the entire sheet row, not the entire row of A:D. Not sure which is the desired behaviour, but that hasn't changed between 2003 and 2010.
 
Upvote 0
@TTTon1

Is the range in question a normal range, or a Table?
 
Upvote 0
That would delete the entire sheet row, not the entire row of A:D. Not sure which is the desired behaviour, but that hasn't changed between 2003 and 2010.
Yep, not sure if you saw the amendment to my post (we may have crossed in posting).

I ran some tests where I had empty cells in columns A:D, but data out in further off columns. So it deleting the blanks, shifting the data from the other columns over into columns A:D.
I figured that is not what they probably want, but am not 100% certain as to their data structure and desired goals.
(And unfortunately, I do not have access to Excel 2003 to verify the behavior there).
 
Upvote 0
Sorry for not beeing clear. I dont want to delete the entire row of the sheet, only the cells within the range if the whole row within the range is blank.

I hope this makes sense.

The only way that would happen is if there is at least one non-empty cell in each of the rows. That code will delete completely empty rows in 2010.

I tested it by running it for each column and it works but once I add another column it wont.
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

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