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
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 26

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

739 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