Consulting

Results 1 to 2 of 2

Thread: Moving rows when cell value is changed

  1. #1
    VBAX Contributor
    Joined
    Jul 2011
    Location
    Manchester
    Posts
    142
    Location

    Moving rows when cell value is changed

    Hi

    Can anyone help with this piece of code please. I want an entire row to move to a separate worksheet in the same work book when a cell value is marked as completed (the sheet it moves to is called completed too). The code does work but; when i click into the cell i get a runtime error before making any changes and i just need it to do nothing unless "COMPLETED" is selected from a drop down list in column 6

    Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'Moves completed cases
    Dim lngNextRow As Long
        If Target.Cells.Count > 1 Then Exit Sub
        'Change column number as required
        If Target.Column = 6 And Target.Row > 1 Then
            'This looks for first empty cell in column A, amend as required
            lngNextRow = Sheets(Target.Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
            Target.EntireRow.Copy Sheets(Target.Value).Range("A" & lngNextRow)
            Target.EntireRow.Delete xlUp
    End If
    End Sub

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Maybe change the SelectionChange to just Change, also add another AND to your If:

    Private Sub Worksheet_Change(ByVal Target As Range) 'Moves completed cases
        Dim lngNextRow As Long
        
        If Target.Cells.Count > 1 Then Exit Sub
        
        'Change column number as required
        If Target.Column = 6 And Target.Row > 1 And Target.Value = "Completed" Then
            'This looks for first empty cell in column A, amend as required
            lngNextRow = Sheets(Target.Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
            Target.EntireRow.Copy Sheets(Target.Value).Range("A" & lngNextRow)
            Target.EntireRow.Delete xlUp
        End If
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •