Report

Code to move rows into another sheet based on certain values

Ask a question nathaliecannell - Last answered on Mar 17, 2017 at 08:02 AM by vcoolio
I'm having trouble figuring out how to do something! I have a sheet (Sheet1) with customer details on it (columns A-D will have data in). I want to be able to write "Cancelled" in column E of the row in particular if the customer's contract has been cancelled and this cause the entire row to be cut and pasted into Sheet2 which will be for cancelled customers with the same column headers etc.

How do I do this? I'm not very good with Macro codes & where to input them!
Helpful
+0
plus moins
Hello Nathaliecannell,

You could try the following code:-


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub
If Target.Value = vbNullString Then Exit Sub
If Intersect(Target, Columns("E:E")) Is Nothing Then Exit Sub

Application.ScreenUpdating = False

        If Target.Value = "Cancelled" Then
        Target.EntireRow.Copy Sheet2.Range("A" & Rows.Count).End(3)(2)
        Target.EntireRow.Delete
        End If
  
Sheet2.Columns.AutoFit

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub


The code is a Worksheet_Change event and needs to be placed in the Worksheet (Sheet1) module. Once "Cancelled" is typed into a cell in Column E of Sheet1 and you click away (or press Enter or down arrow), the relevant row of data will be transferred to Sheet2 and the row will be deleted from Sheet1.

To implement the code, right click on the Sheet1 tab and select "View Code" from the menu that appears. In the big white field that then appears, paste the above code.

Test the code in a copy of your work book first.

I hope that this helps.

Cheerio,
vcoolio.
Leave a comment

Member requests are more likely to be responded to.

Members can monitor the statuses of their requests from their account pages.

A CCM membership gives you access to additional options.

Not a member yet?

Sign up now. It takes less than a minute and is completely free!