Excel VBA Deleting Blank Rows

RajiSingh

New Member
Joined
May 3, 2016
Messages
7
Hi Guys

I am trying to delete blank rows from my worksheet.
The code works but I wanted to start this 5 rows down (This deletes my header)
Could you please help?

Thanks R


Sub d()


Dim rngBlanks As Range
Dim i As Integer
For i = 2 To 3
On Error Resume Next
Set rngBlanks = Columns(i).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not rngBlanks Is Nothing Then
rngBlanks.EntireRow.Delete
End If
Next


End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
use the following code


Dim i As Long



'We turn off calculation and screenupdating to speed up the macro.

With Application

.Calculation = xlCalculationManual

.ScreenUpdating = False



'We work backwards because we are deleting rows.

For i = Selection.Rows.Count To 5 Step -1

If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then

Selection.Rows(i).EntireRow.Delete

End If

Next i



.Calculation = xlCalculationAutomatic

.ScreenUpdating = True

End With
 
Upvote 0
Your code currently deletes all rows in Column B with blanks and then moves to column C and repeats the process. Is this what you are trying to do? If your Headers in column B or C have text then they should not be getting deleted. I just ran a few tests and my Headers remained after the sub procedure has run.

Can you please provide a small sample of your data and possibly demonstrate your end goal by providing a before and after example?
 
Last edited:
Upvote 0
Try this:
Code:
Sub d()

    Dim rngBlanks As Range
    Dim delRange As Range
    Dim i As Integer

    For i = 2 To 3
        On Error Resume Next
        Set rngBlanks = Columns(i).SpecialCells(xlCellTypeBlanks)
        Set delRange = Intersect(rngBlanks, Rows("5:" & Rows.Count))
        On Error GoTo 0
        If Not rngBlanks Is Nothing Then rngBlanks.EntireRow.Delete
    Next

End Sub
But as Matt pointed out, because you are doing columns B and C independently, it might not actually do what you want. That part may be a bit problematic, depending on what your data looks like, and what you actually want it to do.
 
Last edited:
Upvote 0
Hey!!!

Thanks for the help

This is a sample of my report

Portfolio ClassSec AsOf - Run on 12/20/2016
As Of Date: 7/31/2016 Class Scheme: Region Group Scheme Return Type: GROSS OF FEES
Report Measure: Return Currency: BAS Annualized: Yes
Long/Short: Both Portfolio Filter: MONTHLY REPORTS
Class/SecurityEnd WeightEnding Market ValueEnd Date
x portfolio100.00988,837,064.877/31/2016
Cash & Equivalents5.5054,338,079.417/31/2016
Cash Only3.4133,754,571.887/31/2016
Equivalents Only2.0820,583,507.537/31/2016
Equity Total94.50934,498,985.467/31/2016
Pacific13.88137,216,969.957/31/2016
Australia0.959,358,646.377/31/2016
Hong Kong3.8638,202,614.507/31/2016
Japan6.1360,574,992.547/31/2016
Singapore2.9429,080,716.547/31/2016
Europe30.06297,283,076.787/31/2016
Euro11.20110,791,725.957/31/2016
Finland0.848,334,253.157/31/2016
France2.4824,502,151.397/31/2016
Germany1.8818,561,419.047/31/2016
Greece7/31/2016
Holland2.9128,786,398.187/31/2016
Ireland1.7116,957,974.387/31/2016
Italy0.222,143,618.387/31/2016
Spain1.1611,505,911.437/31/2016
Denmark7/31/2016
Non Euro18.86186,491,350.837/31/2016
Norway1.4614,436,141.287/31/2016
Switzerland5.9158,419,077.077/31/2016
United Kingdom11.49113,636,132.487/31/2016
Emerging Markets9.2691,529,814.607/31/2016
Brazil0.999,838,373.227/31/2016
China0.929,065,659.417/31/2016
Hungary7/31/2016
India2.2522,234,014.907/31/2016
Korea2.6626,278,319.857/31/2016
Malaysia7/31/2016
Mexico7/31/2016
Panama0.656,395,582.827/31/2016
South Africa1.7917,717,864.407/31/2016
North America41.31408,469,124.137/31/2016
United States41.31408,469,124.137/31/2016

<tbody>
</tbody>


I did not want to include the header.. but pretty much any country that does not have a end weight and market value, delete the row
 
Upvote 0
Is it always the case where they are either missing BOTH End Weight and Ending Market Value or neither?
In other words, it is ever possible to only be missing one of these fields?

If not, there is no need to check both columns, you would only need to check one.
 
Upvote 0
This should do that without any need for loops (which can slow down procedures and should be avoided, if possible):
Code:
Sub d()

    Dim delRange As Range

    'Identify all blank cells in 2nd column from row 5 on down and delete them
    Set delRange = Intersect(Columns(2).SpecialCells(xlCellTypeBlanks), Rows("5:" & Rows.Count))
    If Not delRange Is Nothing Then delRange.EntireRow.Delete

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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