Solved

How delete rows if no data exists after column A

Posted on 2012-03-26
7
241 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect!  Thank you.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

744 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