Solved

How delete rows if no data exists after column A

Posted on 2012-03-26
7
253 Views
Last Modified: 2012-03-26
I have a spreadsheet that has several rows and columns of data.  But many of the rows have no data AFTER column A.  All rows do have data in column A.  I want to create code to delete all rows that have no data from column B through the end of the file left to right.

Can someone help with this?

--Steve
0
Comment
Question by:SteveL13
  • 4
  • 3
7 Comments
 

Author Comment

by:SteveL13
ID: 37766913
One thing I forgot to mention.  I will want the macro code to start from a designated row going down.  No rows should be deleted above that designated row.
0
 
LVL 41

Expert Comment

by:dlmille
ID: 37766978
If you don't want to touch column A, only delete data from Column B to the right, then:

Sub doTheWork()
Dim wks As Worksheet
Dim rng As Range
Dim lastCol As Long
Dim xInput As Long

    Set wks = ActiveSheet
    
    xInput = InputBox("Enter Row from which to start deleting", 1)
    
    lastCol = wks.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
    
    if lastCol > 1 Then wks.Range("B" & xInput, wks.Cells(wks.Rows.Count, lastCol)).Clear
    
End Sub

Open in new window


Note, I'm using the .clear method as its more efficient, removing data/formats, as opposed to deleting and having Excel shift new cells in.

If you must have .delete, then use:

Sub doTheWork()
Dim wks As Worksheet
Dim rng As Range
Dim lastCol As Long
Dim xInput As Long

    Set wks = ActiveSheet
    
    xInput = InputBox("Enter Row from which to start deleting", 1)
    
    lastCol = wks.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
    
    if lastCol > 1 Then wks.Range("B" & xInput, wks.Cells(wks.Rows.Count, lastCol)).Delete shift:=xlToLeft
    
    
End Sub

Open in new window


cheers,

Dave
0
 

Author Comment

by:SteveL13
ID: 37767081
Not quite right.  I want the entire row deleted if there is nothing after column A.  Then move the remaining rows up.  I may not have been clear enough.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 41

Expert Comment

by:dlmille
ID: 37767127
Got it
Option Explicit

Sub doTheWork()
Dim wks As Worksheet
Dim rng As Range
Dim rDelete As Range
Dim lastCol As Long
Dim xInput As Long
Dim r As Range

    Set wks = ActiveSheet
    
    xInput = InputBox("Enter Row from which to start deleting", 1)
    
    lastCol = wks.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
    
    Set rng = wks.Range("A" & xInput, wks.Range("A" & wks.Rows.Count).End(xlUp))
    
    For Each r In rng
        'check last column in this row, if > 1 then delete entire row
        lastCol = wks.Rows(r.Row).Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
        If lastCol = 1 Then
            If rDelete Is Nothing Then
                Set rDelete = r
            Else
                Set rDelete = Union(rDelete, r)
            End If
        End If
    Next r
    
    If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
End Sub

Open in new window


See attached.

Dave
delNothingAfterColA-r1.xls
0
 

Author Comment

by:SteveL13
ID: 37767222
I said one thing wrong.. Sorry.  I want the entire row deleted if there is nothing after column B, not A.  Then move the remaining rows up.  Now what would the code be?
0
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
ID: 37767259
Ok, we can just check the lastCol parameter (last Column) and if it is <= 2 then there's only data up to column B and we can delete the row, correct?

Option Explicit

Sub doTheWork()
Dim wks As Worksheet
Dim rng As Range
Dim rDelete As Range
Dim lastCol As Long
Dim lastRow As Long
Dim xInput As Long
Dim r As Range

    Set wks = ActiveSheet
    
    xInput = InputBox("Enter Row from which to start deleting", Default:=1)
    
    lastRow = wks.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    
    Set rng = wks.Range("A" & xInput, wks.Range("A" & lastRow))
    
    For Each r In rng
        'check last column in this row, if <=2  then delete entire row
        On Error Resume Next 'in the case of blank rows, then ignore error
        lastCol = wks.Rows(r.Row).Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
        On Error GoTo 0
        If lastCol <= 2 Then
            If rDelete Is Nothing Then
                Set rDelete = r
            Else
                Set rDelete = Union(rDelete, r)
            End If
        End If
    Next r
    
    If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
End Sub

Open in new window

Dave

See attached.

Dave
delNothingAfterColB-r2.xls
0
 

Author Closing Comment

by:SteveL13
ID: 37767825
Perfect!  Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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 will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

920 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

16 Experts available now in Live!

Get 1:1 Help Now