Solved

How delete rows if no data exists after column A

Posted on 2012-03-26
7
275 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
[X]
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
  • 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 42

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 42

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 42

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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.

739 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