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
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
363 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 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Excel 201 rounding creating a problem with my macro 11 34
Web Query 1 20
Collapse and expand table in Word 2010 2 32
Do Wend Macro not working 22 33
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

839 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