Macro to copy cells in sh 2 triggered by an entry in one of them and pasted in sh1 into a row with same log no.

Raceman

Board Regular
Joined
Mar 11, 2010
Messages
64
Hello: can someone help me with VBA Code to do the following:

On Sheet 2, after an entry is made in Cell P an event is triggered that will copy cells P and Q (in the same row) only if Cell C in that row = "Task Summary" (note on sheet 2 there is a log no. in cell A)

then on Sheet 1 this info will be pasted into cells Y and Z (and the row with the same Log No.as on Sheet 2) .

Log No., which is a three digit no. starting with 001, is in Column A on both Sheet 1 and 2.

Not sure where to even start on this one​
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim fVal As Range
If Not Intersect(Target, Range("P:P")) Is Nothing Then
    Set fVal = Sheets("Sheet1").Range("A:A").Find(Range("A" & Target.Row).Value, LookIn:=xlValues)
        If Not fVal Is Nothing Then
            Target.Resize(1, 2).Copy Sheets("Sheet1").Range("Y" & fVal.Row)
        End If
End If
End Sub
 
Upvote 0
Hi: JLGWhiz : Thanks much your code works great when I disable other code on that Worksheet. I'm obviously new at this and don't quite understand how to integrate your code with an existing Private Sub Worksheet_Change on the same Worksheet. Especially not sure how to relate Dim fVBal As Range with my existing code, but as shown below, the code stops with an error when I attempt to get my automatic date (from my code), the error says, ""Compile error: End If without block If".
HTML:
'This will automatically provide dates where appropriate
Private Sub Worksheet_Change(ByVal Target As Range)
Set t = Target
Set dd = Range("$D$3:$D$5000")
Set ff = Range("$F$3:$F$5000")
Set hh = Range("$H$3:$H$5000")
Set jj = Range("$J$3:$J$5000")
Set ll = Range("$L$3:$L$5000")
Set nn = Range("$N$3:$N$5000")
Set pp = Range("$P$3:$P$5000")
If Target.Count > 1 Then Exit Sub
If (Intersect(t, dd) Is Nothing) And (Intersect(t, ff) Is Nothing) And (Intersect(t, dd) Is Nothing) And (Intersect(t, hh) Is Nothing) And (Intersect(t, jj) Is Nothing) And (Intersect(t, ll) Is Nothing) And (Intersect(t, nn) Is Nothing) And (Intersect(t, pp) Is Nothing) Then Exit Sub
Application.EnableEvents = False
t.Offset(0, 1).Value = Date
Application.EnableEvents = True
End If
'This code provided by JLGWhiz automatically moves data from cells P and Q to Sheet 1
Dim fVal As Range
If Not Intersect(Target, Range("P:P")) Is Nothing Then
Set fVal = Sheets("Sheet1").Range("A:A").Find(Range("A" & Target.Row).Value, LookIn:=xlValues)
If Not fVal Is Nothing Then
Target.Resize(1, 2).Copy Sheets("Sheet1").Range("Y" & fVal.Row)
End If
End If
End Sub
 
Upvote 0
Hi JLGWhiz: I got it working- I removed the "End If" between our codes and then it worked. Very cool....thanks so much for your help!
 
Upvote 0
Oh I just realized....your code does the auto copy and paste for every row on sh2, if possible, I needed this macro to be triggered only in rows where Cell C in that row = "Task Summary"
 
Upvote 0
Oh I just realized....your code does the auto copy and paste for every row on sh2, if possible, I needed this macro to be triggered only in rows where Cell C in that row = "Task Summary"

Modified.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Set t = Target
Set dd = Range("$D$3:$D$5000")
Set ff = Range("$F$3:$F$5000")
Set hh = Range("$H$3:$H$5000")
Set jj = Range("$J$3:$J$5000")
Set ll = Range("$L$3:$L$5000")
Set nn = Range("$N$3:$N$5000")
Set pp = Range("$P$3:$P$5000")
If Target.Count > 1 Then Exit Sub
If (Intersect(t, dd) Is Nothing) And (Intersect(t, ff) Is Nothing) And (Intersect(t, dd) Is Nothing) _
And (Intersect(t, hh) Is Nothing) And (Intersect(t, jj) Is Nothing) And (Intersect(t, ll) Is Nothing) _
And (Intersect(t, nn) Is Nothing) And (Intersect(t, pp) Is Nothing) Then Exit Sub
Application.EnableEvents = False
t.Offset(0, 1).Value = Date
Application.EnableEvents = True
    'This code provided by JLGWhiz automatically moves data from cells P and Q to Sheet 1
    Dim fVal As Range
    If Not Intersect(Target, Range("P:P")) Is Nothing Then
        Set fVal = Sheets("Sheet1").Range("A:A").Find(Range("A" & Target.Row).Value, LookIn:=xlValues)
        If Not fVal Is Nothing And Range("C" & fVal.Row) = "Task Summary" Then
        Target.Resize(1, 2).Copy Sheets("Sheet1").Range("Y" & fVal.Row)
    End If    
End Sub
 
Last edited:
Upvote 0
Ok thanks for the quick response, Your latest didn't work until I added another End If at the end, Does this make sense?

HTML:
'This code provided by JLGWhiz automatically moves data from cells P and Q to Sheet 1 but only if Sh2 line says Task Summary
Dim fVal As Range
If Not Intersect(Target, Range("P:P")) Is Nothing Then
Set fVal = Sheets("MAD_CAT").Range("A:A").Find(Range("A" & Target.Row).Value, LookIn:=xlValues)
If Not fVal Is Nothing And Range("C" & fVal.Row) = "Task Summary" Then
Target.Resize(1, 2).Copy Sheets("MAD_CAT").Range("Y" & fVal.Row)
End If
End If
End Sub
 
Upvote 0
Yep, don't know why I deleted that.
Regards, JLG
 
Upvote 0
JLGWhiz:

I'm having major problems with this after I thought it was resolved. I didn't test it well enough I guess...my bad.

But I am not getting the desired result except for row 3 on the active sheet. An entry in column P, with "Task Summary" in C, produces a copy of P and Q to the MAD CAT sheet (Columns Y and Z) but only for the first row with data , which is row 3 on the active sheet.

HTML:
'This will automatically provide dates where appropriate
Private Sub Worksheet_Change(ByVal Target As Range)
Set t = Target
Set dd = Range("$D$3:$D$5000")
Set ff = Range("$F$3:$F$5000")
Set hh = Range("$H$3:$H$5000")
Set jj = Range("$J$3:$J$5000")
Set ll = Range("$L$3:$L$5000")
Set nn = Range("$N$3:$N$5000")
Set pp = Range("$P$3:$P$5000")
If Target.Count > 1 Then Exit Sub
If (Intersect(t, dd) Is Nothing) And (Intersect(t, ff) Is Nothing) And (Intersect(t, dd) Is Nothing) And (Intersect(t, hh) Is Nothing) And (Intersect(t, jj) Is Nothing) And (Intersect(t, ll) Is Nothing) And (Intersect(t, nn) Is Nothing) And (Intersect(t, pp) Is Nothing) Then Exit Sub
Application.EnableEvents = False
t.Offset(0, 1).Value = Date
Application.EnableEvents = True

'This code automatically moves data from cells P and Q to Sheet 1 but only if Sh2 line says Task Summary
Dim fVal As Range
If Not Intersect(Target, Range("P3:P15")) Is Nothing Then
Set fVal = Sheets("MAD_CAT").Range("A:A").Find(Range("A" & Target.Row).Value, LookIn:=xlValues)
If Not fVal Is Nothing And Range("C" & fVal.Row) = "Task Summary" Then
Target.Resize(1, 2).Copy Sheets("MAD_CAT").Range("Y" & fVal.Row)
End If
End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,847
Members
449,194
Latest member
HellScout

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