Worksheet Change VBA Code

kcpettit13

New Member
Joined
Jun 8, 2016
Messages
5
Hello,

I am trying to automatically move an entire row of data to a different sheet based on changing text in a certain column i.e. entering "YES" in column I. I am new to writing code. The code I wrote seems to be skipping some steps. Can anyone help?

Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Sent_to_Tox").Select
ActiveSheet.Range("I2", ActiveSheet.Range("I2").End(xlDown)).Select
If ActiveCell.Text = "YES" Then
Selection.EntireRow.Cut
Sheets("Completed").Select
ActiveSheet.Range("LastCell").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Sent_to_Tox").Select
ActiveCell.Select
Selection.EntireRow.Delete
End If


End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Which sheet do you want to move the row to?

Where on that sheet should the row go?
 
Upvote 0
I want to move the row to the sheet titled "completed". I have a named range in that sheet to designate the last row because data is constantly being added to the sheet and I always want the row to move to the last row.
 
Upvote 0
So this code is for the sheet 'Sent_to_Tox'?

If that's the case all you should need is something like this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column = 9 Then
        If Target.Value = "YES" Then
            Application.EnableEvents = False
            Target.EntireRow.Copy Sheets("Completed").Range("A" & Rows.Count).End(xlUp).Offset(1)
            Target.EntireRow.Delete
            Application.EnableEvents = True
        End If
    End If

End Sub
 
Upvote 0
I was using this code, but the loop took way to long to run.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xrow As Long
xrow = 2
Sheets("Need_to_Complete").Select
Dim lastrow As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row


Do Until xrow = lastrow + 1
ActiveSheet.Cells(xrow, 8).Select
If ActiveCell.Text = "YES" Then
Selection.EntireRow.Cut
Sheets("Completed").Select
ActiveSheet.Range("LastCell").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Need_to_Complete").Select
ActiveCell.Select
Selection.EntireRow.Delete
xrow = xrow - 1
End If

xrow = xrow + 1
Loop



End Sub
 
Upvote 0
That worked! Thank you!

One other question... if I wanted the cell to change when I enter a date in stead of "yes" how would that change the code?
 
Upvote 0

Forum statistics

Threads
1,216,759
Messages
6,132,558
Members
449,736
Latest member
anthx

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