Copy Row with condition

Throughstream

New Member
Joined
Dec 9, 2016
Messages
34
Hi I have the following set up for an annual leave style spreadsheet.

Sheet1
31167965360_54bc17fd22_b.jpg

I would like to copy into sheet2 only the rows that have A under the name, is there a formula or do I have to use vba.

Cheers

Dave
 
Try this:

When you enter some value into column "G" this script will run
It will copy the active row into the sheet named in column "A"

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab named "Master Copy 1"
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("G:G")) Is Nothing Then
On Error GoTo M
Dim ans As String
Dim Lastrow As Long
ans = Cells(Target.Row, 1).Value
Lastrow = Sheets(ans).Cells(Rows.Count, "A").End(xlUp).Row + 1
Rows(Target.Row).Copy
Sheets(ans).Rows(Lastrow).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Exit Sub
M:
MsgBox "No such sheet exist"
End If
End Sub
 
Last edited:
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Now you need to know the minute you enter a value into column "G" the script will run.

So if you put a value in column "G" and then put a value in column "M" the data in column "M" will not be copied over.

And the value put in column "G" must be entered manually and not as the result of a formula.
 
Upvote 0
Just re-read all the posts and I think this might be what you want.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Dim nm As String
Application.EnableEvents = False
    If Not Intersect(Target, Range("G:G")) Is Nothing Then
        nm = Target.Offset(, -6).Value
            If nm <> "" Then
                Target.EntireRow.Copy 
                Sheets(nm).Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial xlPasteValues
            End If
    End If
Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
Try changing this line of code

Code:
ans = Cells(Target.Row, 1).Value

To this:

Code:
ans = Cells(Target.Row, [COLOR=#FF0000]2[/COLOR]).Value

The "2" means column (2)
 
Upvote 0
Hi many thanks, is there a way of it only copying the row if the "date requested" falls between 01/04/2017 (April the first) to 31/03/2018 (end of March). Any date requested out side wouldn't get copied but wouldn't come up with an error.

Cheers

Dave
 
Upvote 0

Forum statistics

Threads
1,223,577
Messages
6,173,163
Members
452,503
Latest member
AM74

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