Solved

Delete row if cell in Column B is blank

Posted on 2011-03-23
3
569 Views
Last Modified: 2012-05-11
I have a worksheet with data, but if the cell in column B is blank, I want the entire row to be deleted.  I have the below, but since column A has data, it will not delete the row.  This must be done in the VBA module.
Sub DeleteBlankRows(strWbkName As String)
'Deletes the entire row within the selection if the ENTIRE row contains no data.
'We use Long in case they have over 32,767 rows selected.
Dim i As Long
With Workbooks(strWbkName)
    .Activate
With .Worksheets("DataSource")
    .Activate
    .Range("A1:DD630").Select
    'We work backwards because we are deleting rows.
    For i = Selection.Rows.Count To 1 Step -1
        If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
            Selection.Rows(i).EntireRow.Delete
        End If
    Next i
    End With
End With
End Sub

Open in new window

0
Comment
Question by:ssmith94015
3 Comments
 
LVL 9

Accepted Solution

by:
sshah254 earned 500 total points
Comment Utility
Shouldn't the code be something like ...

If .Range("B"&i) == "" Then
  Selection.Rows(i).EntierRow.Delete
End If

Ss
0
 

Author Closing Comment

by:ssmith94015
Comment Utility
Yes, that was it.  I was missing the fact that it had to actually check the cell in column B each time.
0
 
LVL 50

Expert Comment

by:Dave Brett
Comment Utility
While this is closed you can do this much more efficiently without a loop - if the B cells are truly blank

to delete all rows on the current sheet where B cells are blank

hth

Dave
Sub QuickKill()
On Error Resume Next
Columns("B").SpecialCells(xlBlanks).EntireRow.Delete
End Sub

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

763 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

6 Experts available now in Live!

Get 1:1 Help Now