Copy and paste row to new sheet based on cell value

jjt1973

New Member
Joined
Jun 8, 2011
Messages
32
I am trying to find a macro that will allow me to copy the contents of a row to another sheet2 based on a cell value in certain columns... Here is my example:

1661282633109.png


For this example I am looking to copy the row where the status (Column D) equals "Closed" to sheet2. I also need it to auto update as I enter in more rows of information.

Thank you in advance
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hello,

The code bello will work.

NB activate it on Sheet1.

VBA Code:
Sub CopyRowsToNewSheet()
Dim i As Integer
Dim j As Integer
    j = 1
    Application.ScreenUpdating = False
    Sheets("Sheet2").Range("A1:D22").ClearContents  'Clears Sheet2 Data
    For i = 1 To 22 '22 Number of rows to work through
    If Cells(i, 4).Value = "Closed" Then
    Cells(i, 4).Select
    Rows(ActiveCell.Row).Copy
    Sheets("Sheet2").Select
    Cells(j, 1).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    j = j + 1
    Sheets("Sheet1").Select
    End If
    Next i
    Range("A1").Select
    Application.ScreenUpdating = True
    Range("A1").Select
End Sub

Jamie
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your data sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter "Closed" in any row in column D and press the ENTER key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 4 Then Exit Sub
    If Target = "Closed" Then
        With Sheets("Sheet2")
            Target.EntireRow.Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    End If
    Application.ScreenUpdating = False
End Sub
 
Upvote 0
Solution
Hello,

The code bello will work.

NB activate it on Sheet1.

VBA Code:
Sub CopyRowsToNewSheet()
Dim i As Integer
Dim j As Integer
    j = 1
    Application.ScreenUpdating = False
    Sheets("Sheet2").Range("A1:D22").ClearContents  'Clears Sheet2 Data
    For i = 1 To 22 '22 Number of rows to work through
    If Cells(i, 4).Value = "Closed" Then
    Cells(i, 4).Select
    Rows(ActiveCell.Row).Copy
    Sheets("Sheet2").Select
    Cells(j, 1).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    j = j + 1
    Sheets("Sheet1").Select
    End If
    Next i
    Range("A1").Select
    Application.ScreenUpdating = True
    Range("A1").Select
End Sub

Jamie
Just a little heads up (a tip on improving VBA code).
Having a lot of "Select" statements in your code can really slow it down (especially if inside of a loop!).
Usually, you don't need to select a range in order to work with it, and it is much faster to leave them out, whenever possible.
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your data sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter "Closed" in any row in column D and press the ENTER key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 4 Then Exit Sub
    If Target = "Closed" Then
        With Sheets("Sheet2")
            Target.EntireRow.Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    End If
    Application.ScreenUpdating = False
End Sub
This worked great!! Thank you!
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your data sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter "Closed" in any row in column D and press the ENTER key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 4 Then Exit Sub
    If Target = "Closed" Then
        With Sheets("Sheet2")
            Target.EntireRow.Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    End If
    Application.ScreenUpdating = False
End Sub
If I wanted to alter the code so that if Column D reflected "Cancelled" that row would be copied to sheet 3.. Could I or would it be a whole different code? I ask because I tried adding an additional "If" statement using the same code and it didn't work.
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 4 Then Exit Sub
    Select Case Target.Value
        Case "Closed"
            With Sheets("Sheet2")
                Target.EntireRow.Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
            End With
        Case "Cancelled"
            With Sheets("Sheet3")
                Target.EntireRow.Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
            End With
    End Select
    Application.ScreenUpdating = False
End Sub
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 4 Then Exit Sub
    Select Case Target.Value
        Case "Closed"
            With Sheets("Sheet2")
                Target.EntireRow.Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
            End With
        Case "Cancelled"
            With Sheets("Sheet3")
                Target.EntireRow.Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
            End With
    End Select
    Application.ScreenUpdating = False
End Sub
[/QUOTE]


This worked great!! Thank you again!
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,509
Members
449,089
Latest member
RandomExceller01

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