add a line to this code to delete rows

Status
Not open for further replies.

abound1

Board Regular
Joined
Aug 8, 2005
Messages
84
The following code searches column A for any cells containing a keyword, and moves those cells 1 row up and 2 columns to the right.

I'd like to delete the row that the moved cell used to occupy. Can someone please add a line to the code that will do that.
Code:
Sub MoveCells() 
For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row) 
    If Len(WorksheetFunction.Substitute(c, "KEYWORD", "")) <> Len(c) Then 
        c.Cut c.Offset(-1, 2) 
    End If 
Next c 
End Sub
Thanks
 

Some videos you may like

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Joined
Jul 30, 2006
Messages
3,656
Try this on a test sheet:

Code:
Sub MoveCells()
For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    If Len(WorksheetFunction.Substitute(c, "KEYWORD", "")) <> Len(c) Then
        c.Copy c.Offset(-1, 2)
        c.EntireRow.Delete
    End If
Next c
End Sub


Have a great day,
Stan
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi,

this last code will not handle all instances if there are "keywords" in consecutive rows
a little example to show, what happens when deleting consecutive rows
Code:
Sub wrong_delete_loop()

Dim x As Integer

    'put 1 to 5 in A1 to A5
    For x = 1 To 5
    Cells(x, 1) = x
    Next x
    
    'expect to delete first 5 rows ?
    For x = 1 To 5
    Rows(x).Delete
    Next x

    'now see what happened !
End Sub
two ways of solving
1. after each "delete" substract 1 from counter
2. looping backwards
example of second solution
Code:
Sub MoveCells()
Dim LR As Long
Dim i As Long

LR = Cells(Rows.Count, 1).End(xlUp).Row
    For i = LR To 2 Step -1
        With Cells(i, 1)
            If Len(WorksheetFunction.Substitute(.Value, "KEYWORD", "")) <> Len(.Value) Then
                .Copy .Offset(-1, 2)
                .EntireRow.Delete
            End If
        End With
    Next i

End Sub
kind regards,
Erik
 
Joined
Jul 30, 2006
Messages
3,656
We might have a problem with both pieces of code.

If there are entries with "keyword" in consecutive rows, then the next 'keyword' in the list will overwrite the information in column C, of the previous row.


Have a great day,
Stan
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,453

ADVERTISEMENT

We might have a problem with both pieces of code.
Actually, unless there will never be any consecutive rows containg the keyword, there
is a problem with the logic of the request.
Once the keywords are moved up & over, by deleting the rows they came from the second
(and all subsequent) consecutive keywords will be deleted as well.

If there will never be any consecutive keywords then I think this should work.
Code:
Sub MoveCells()
Dim Rng As Range
Set Rng = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
For Each c In Rng
  If Len(WorksheetFunction.Substitute(c, "KEYWORD", "")) <> Len(c) Then
    c.Cut c.Offset(-1, 2)
  End If
Next c
Rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
This should be OK unless you expect more than (I think) 9,281 non consecutive keywords.
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
We might have a problem with both pieces of code.

If there are entries with "keyword" in consecutive rows, then the next 'keyword' in the list will overwrite the information in column C, of the previous row.


Have a great day,
Stan
Your right ! In fact it's not overwritten, it's deleted on next loop :oops:

EDIT: didn't check HalfAces reply before posting: that's to the point, Dan :biggrin:
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Just found out this was a duplicate thread :x

http://www.mrexcel.com/board2/viewtopic.php?p=1139610&highlight=#1139610

This is against the boardrules
I do not want to play "backseat-moderator" but in this particular moment, I find it unpleasant.

It would have been more constructive to join in the same thread instead of taking the time of people from diverse sides
(don't know if this is good english, but I think you've got the message !)

greetings from Belgium,
Erik
 
Status
Not open for further replies.

Watch MrExcel Video

Forum statistics

Threads
1,113,918
Messages
5,545,025
Members
410,647
Latest member
bernardazar
Top