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
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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:
 
Upvote 0
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
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,213,522
Messages
6,114,112
Members
448,549
Latest member
brianhfield

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top