|
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?
I have been using the following code to delete empty rows from the current sheet and at times it causes Excel to hang. Is it the fault of the code and if so is there a better way to perform the same function?
Code:
Sub RemoveRowsEmpty()
' Macro recorded 10/12/2006 by Jon
LastRow = ActiveSheet.UsedRange.Rows.Count
LastRow = LastRow + ActiveSheet.UsedRange.Row - 1
Application.ScreenUpdating = False
For r = LastRow To 1 Step -1
If Application.CountA(Rows(r)) = 0 Then Rows(r).Delete
Next r
End Sub
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.
I apologise if this topic has been covered before but I just could not find exactly what I require when I searched the message board.
I have a Matrix consisting of 20 Columns and 1000's of Rows.
I am looking for a Macro that will remove all Rows that do not have Data in Columns B through to the nth Column (20 would be enough).
I do have Data in Column A for every row but if there isn't any data across the row in any other column then it is to be deleted.
SP
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 there in sheet1 I have a "IF" formula that deletes information if it has a "YES" on the "loans" tab...
is it possible that if the information gets deleted to have the rows do the same??? that way everything is grouped together???
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, I received many spreadsheets that are in the same format but all of them contain empty rows in particular columns. Like the exampe below, I want to delete the row that has no value in ColC:
ColA ColB ColC
A B $20
B B
I like to know how I can delete the empty rows automatically instead of manually deletion. I am not proficient in VBA but I can understand the codes if there are some examples. Any help is greatly appreciated!
Thanks,
Howdy,
|
|