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
Solved

How delete rows if no data exists after column A

Posted on 2012-03-26
7
266 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 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

856 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