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
361 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
ID: 40402769
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 24

Assisted Solution

by:NVIT
NVIT earned 100 total points
ID: 40402773
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
ID: 40402827
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
ID: 40404901
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
ID: 40407521
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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