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
368 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

695 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