Edit Code for deleting row after Cut/Paste Macro

nairion

New Member
Joined
Dec 13, 2016
Messages
18
I am attempting to have a macro cut a row from one worksheet to another whenever a cell in Column G is changed to "Closed."

The Code I have operating is: (supplied by helpful forum regulator #shoutout My Answer is This)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G:G")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Dim ans As Long
Dim Lastrowa As Long
Lastrowa = Sheets("Closed Projects").Cells(Rows.Count, "G").End(xlUp).Row + 1
If Target.Value = "Closed" Then Rows(Target.Row).Cut Destination:=Sheets("Closed Projects").Rows(Lastrowa)
End If
End Sub
When this works its magic, it effectively copies the row to the next page instead of cutting the row. It leaves empty rows in its wake that have to be manually deleted.

Thoughts?

Also, does this code work after a filter has been applied to the data?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Not Intersect(Target, Range("G:G")) Is Nothing Then
        If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
        On Error GoTo errHandler
        If Target = "Closed" Then
            Target.EntireRow.Copy Sheets("Closed Projects").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            Target.EntireRow.Delete
        End If
    End If
errHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hello Nairion, see if the code below will work for you. I'm relatively sure that it will work with filtered data, but you'll want to test it out to see if it works for you.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ans&, Lastrowa&, CutRow&

    If Not Intersect(Target, Range("G:G")) Is Nothing Then
        If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
        Lastrowa = Sheets("Closed Projects").Cells(Rows.Count, "G").End(xlUp).Row + 1
        If Target.Value = "Closed" Then
            CutRow = Target.Row
            Application.EnableEvents = False
            Rows(CutRow).Cut Destination:=Sheets("Closed Projects").Rows(Lastrowa)
            Rows(CutRow).Delete
            Application.EnableEvents = True
    End If
End Sub
 
Last edited:
Upvote 0
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Not Intersect(Target, Range("G:G")) Is Nothing Then
        If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
        On Error GoTo errHandler
        If Target = "Closed" Then
            Target.EntireRow.Copy Sheets("Closed Projects").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            Target.EntireRow.Delete
        End If
    End If
errHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

This one worked for me. Thank you
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,737
Members
449,050
Latest member
excelknuckles

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