Solved

How delete rows if no data exists after column A

Posted on 2012-03-26
7
260 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Real Time 2 21
Remove duplicates using cell values VBA 2 28
Excel formula Sumif not working 4 26
VBA Help 18 42
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

816 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

12 Experts available now in Live!

Get 1:1 Help Now