VBA Code to copy a line based on criteria

wlbamc

Board Regular
Joined
Apr 19, 2016
Messages
99
Office Version
  1. 2016
Hi, I want to automatically copy a line in Excel from one worksheet to another if the option Fast Track is chosen in column P. I currently have the following which copies the columns I need, but it copies every line not just the Fast Track ones.

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub

With Application
  .EnableEvents = False
  .ScreenUpdating = False
End With

Select Case Target.Column

  Case Is = 1
    Target.Copy
       Sheets("Pending").Range("A" & Rows.Count).End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
       Target.Offset(, 1).Select
       
  Case Is = 2
    Target.Copy
       Sheets("Pending").Range("B" & Rows.Count).End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
       Target.Offset(, 1).Select
       
  Case Is = 5
    Target.Copy
       Sheets("Pending").Range("C" & Rows.Count).End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
       Target.Offset(, 1).Select
       
  Case Is = 6
    Target.Copy
       Sheets("Pending").Range("G" & Rows.Count).End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
       Target.Offset(, 1).Select
       
  Case Is = 7
    Target.Copy
       Sheets("Pending").Range("H" & Rows.Count).End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
       Target.Offset(, 1).Select

  Case Is = 8
    Target.Copy
       Sheets("Pending").Range("I" & Rows.Count).End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
       Target.Offset(, 1).Select
       
    Case Is = 14
    Target.Copy
       Sheets("Pending").Range("O" & Rows.Count).End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
       Target.Offset(1, 1).Select
       
Case Else
    '
       
End Select


With Application
  .EnableEvents = True
  .CutCopyMode = False
  .ScreenUpdating = True
End With

End Sub

Many thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
There is no "Fast Track" specified in your code...
Try this code :
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column <> 16 Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target.Value <> "Fast Track" Then Exit Sub

Dim last As Long
    last = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row + 1
    Sheet2.Range("A" & last & ":" & "P" & last).Value = Sheet1.Range("A" & Target.Row & ":" & "P" & Target.Row).Value

End Sub
 
Upvote 0
There is no "Fast Track" specified in your code...
Try this code :
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column <> 16 Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target.Value <> "Fast Track" Then Exit Sub

Dim last As Long
    last = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row + 1
    Sheet2.Range("A" & last & ":" & "P" & last).Value = Sheet1.Range("A" & Target.Row & ":" & "P" & Target.Row).Value

End Sub
This has only copied through Column P not the rest of the columns which my original code did, am I suppose to be adding this to my code?
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,209
Members
448,874
Latest member
b1step2far

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