Solved

Excel VBA - Help with 1.  testing for a completely blank row, 2.  deleting that blank row.  3.  moving down one line. 4. repeating

Posted on 2014-10-24
5
356 Views
Last Modified: 2014-11-13
I would like to have VBA
1.  Detect if the current row is blank.
2.  delete the entire row if its blank.
3.  move down one row
4.  go to step 1.
0
Comment
Question by:brothertruffle880
5 Comments
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 100 total points
Comment Utility
here is the code:

i = 1
j = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Do While i < j

If ActiveSheet.Range("A" & i).Value = "" Then
ActiveSheet.Rows(i).Delete
i = i - 1
j = j - 1
Else
i = i + 1
End If

Loop

Open in new window

0
 
LVL 23

Assisted Solution

by:NVIT
NVIT earned 100 total points
Comment Utility
Do you want to delete all blank rows? If so, maybe this (under "How about"): http://stackoverflow.com/questions/9379673/excel-vba-delete-empty-rows
0
 
LVL 27

Assisted Solution

by:Glenn Ray
Glenn Ray earned 100 total points
Comment Utility
This will check all values in your active area and only remove rows that are entirely blank.  This code is based on a solution by dlmille in Experts Exchange
Sub Delete_Blank_Rows()
    'based on code from dlmille, Experts Exchange Q_27636554
    Dim rng As Range
    Dim r As Range
    Dim chkRow As String
    Dim rng2 As Range
    Dim r2 As Range
    Dim rDelete As Range
    Dim lngLR, lngLC As Long

    lngLR = Cells.SpecialCells(xlLastCell).Row
    lngLC = Cells.SpecialCells(xlLastCell).Column
    
    Set rng = Range("A2", Cells.SpecialCells(xlLastCell))
    
    For Each r In rng
        Set rng2 = Range(Cells(r.Row, 1), Cells(r.Row, lngLC))
        For Each r2 In rng2
            chkRow = chkRow & Trim(r2.Value)
        Next r2
        If chkRow = vbNullString Then
            If rDelete Is Nothing Then
                Set rDelete = r
            Else
                Set rDelete = Union(r, rDelete)
            End If
        Else
            chkRow = vbNullString
        End If
    Next r
    If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
End Sub

Open in new window


It does not literally iterate through each row as you described, but the result is as-requested.

Regards,
-Glenn

PS Haris' code only checks cells in column A.
EE-DeleteBlankRows.xlsm
0
 
LVL 4

Assisted Solution

by:yuppydu
yuppydu earned 100 total points
Comment Utility
Try this. There is a light user interface to specify the row range to clean empty rows. Keep in mind there is no error check routine beside a basic consistency check for the input.
delete-rows.xlsm
0
 
LVL 25

Accepted Solution

by:
ProfessorJimJam earned 100 total points
Comment Utility
here you go simple as this


Sub DeleteBlankRow()
Dim row As Range
Dim sheet As Worksheet
Set sheet = ActiveSheet



For i = sheet.UsedRange.Rows.Count To 1 Step -1

    Set row = sheet.UsedRange.Rows(i)
    If WorksheetFunction.CountA(row) = 0 Then
        Rows(i).EntireRow.Delete
    End If

Next i
End Sub

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now