|
Delete Rows in Excel if Completely Empty
This macro will delete only completely blank rows in an excel spreadsheet. It allows you to make a selection of rows, run the macro, and then will delete only rows that have no data.
This is a great macro if you receive data that has only some empty cells but the columns next to the empty cells are still important.
Where to install the macro: Module
Delete Completely Blank Rows
Public Sub DeleteCompletelyBlankRows()
Dim R As Long
Dim C As Range
Dim N As Long
Dim Rng As Range
On Error GoTo EndMacro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
If Selection.Rows.Count > 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If
N = 0
For R = Rng.Rows.Count To 1 Step -1
If Application.WorksheetFunction.CountA(Rng.Rows(R).EntireRow) = 0 Then
Rng.Rows(R).EntireRow.Delete
N = N + 1
End If
Next R
EndMacro:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007 Go to Tools > Macros > Visual Basic Editor
For Excel 2007 Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.
Similar Helpful Excel Resources
I have 24 worksheets..
All are named in the same manner...
"AISLE A EVEN"
"AISLE A ODD"
Through AISLE L ( So A-L ).
Here goes nothing...
For every sheet, I need to delete any row that meets these 2 criteria:
Col E The Aisle's worksheet "aisle"
Col H The Aisle's worksheet EVEN ( or odd ).
This will be mind blowing.... if someone can crack it....
So a run-down...
On the worksheet "Aisle A EVEN"
It will delete any row that Col E A, and Col H EVEN
and so forth...
e.g.:
B A O P
Q L P E
Q D L E
if i press Ctrl+End, it will jump to the E at bottom right corner
then
i delete the last row(entire)
and press Ctrl+End, it jumps to the empty space(previously the last "E")
how can I solve that problem?
thx a lot!
I'm trying to delete the empty rows as well as the rows that have empty cell in specific column?
Greetings, I'm currently running this code when the sheet deactivates to delete empty rows from a worksheet:
Code:
Private Sub Worksheet_Deactivate()
Application.ScreenUpdating = False
Sheets("Records").Unprotect Password:="secret"
With Worksheets("Records")
.AutoFilterMode = False
With Range("B2", Range("B" & Rows.Count).End(xlUp))
.AutoFilter 1, "*Nil*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
Sheets("Records").AutoFilterMode = False
End With
Sheets("Records").Protect Password "secret"
Application.ScreenUpdating = True
End Sub
The problem is, if there are less than two records in the worksheet, it will delete the heading row, breaking my dynamic named ranges and tables based on same.
I tried adding a check like this:
Code:
If ActiveSheet.UsedRange.Rows.Count < 3 Then Exit Sub
But this doesn't seem to work. Appreciate code / ideas to help!
EDIT: I should add that a new row is generated automatically when the last record has been completed, in anticipation of the user needing to enter another record. But if they don't actually need to enter anything, then I have to remove that blank row so that my dynamic named ranges don't include it in the range, thus messing up tables/pivot tables.
Could someone please tell me how to delete all empty rows in a spread sheet? I would like the rows to be deleted ONLY if ALL columns in the row are empty.
Thank you!
Hi, someone on this board helped me with the macro below, basically it's deleting the row (only 1 row) above the "custom table" rows, it worked the the other day, but not sure when i put it in a new file, it's not working properly. could someone advise on this? thanks much!
Code:
Sub DelEmptyRowAboveCustomTables()
'this macro should delete one row above each "custom tables", good luck!
Dim LastRow As Long, i As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For i = 1 To LastRow
With Cells(i, "A")
If .Value = "Custom Tables" And .Offset(2).Value Like "Table*" Then
.Offset(-1).EntireRow.Delete
End If
End With
Next i
Application.ScreenUpdating = True
End Sub
Hi !
How to delete activerow + two second rows ?
examble A colunm B column
row 1 Hold
2
3
row 1 hold
2 aasas (Don't delete and find next HOLD)
3
First I have to find "Hold" from B column and then lookup is the value A column second row empty ,if it's empty then delete all tree rows and then find Hold from column B and do the same.
Values in column B should be somenthing else than Hold
Br
Timo
Hi,
I have the following procedure which will delete entirely empty rows. However I only want to delete the row if every cell after column B is empty. I can't figure out how to modify the code to do this.
The code...
Code:
Sub DelEmptyRows()
Dim i As Long, lngLimit As Long
lngLimit = ActiveSheet.UsedRange.Rows.Count
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For i = lngLimit To 1 Step -1
If Application.CountA(Cells(i, 1).EntireRow) = 0 Then
Cells(i, 1).EntireRow.Delete
End If
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
lngLimit = ActiveSheet.UsedRange.Rows.Count 'attempt to fix lastcell
ActiveWorkbook.Save
End Sub
Any help would be appreciated.
Regards,
Hi,
I am trying to delete rows that are zero or blank. I am not sure why the various VBA codes I have tried doesn't work. It may have to do with the cells having formulas.
In the attached file excerpt, I am trying to delete rows with zero or blank in column P. However, column P has formulas.
The actual sheet would have over 6,500 rows.
Thanks.
Does anyone know how I can delete empty rows using VBA?
Thanks in advance.
|
|