Forum Discussion
kaanman
Sep 30, 2020Copper Contributor
Excel Trigger on change help
I have a lot to learn with Excel, so here's what I need if anyone can help me. For work, I upkeep a excel spreadsheet for project status, but also have a history with finished projects. I ba...
- Oct 01, 2020
Here is a new version:
Private Sub Worksheet_Change(ByVal Target As Range) ' The other sheet where you want to store the row(s) Const strOther = "OtherSheet" ' The column with Y/N Const strYN = "M" ' The column for the time stamp Const strStamp = "T" ' Variables Dim wsh As Worksheet Dim rng As Range ' Check whether the Y/N column has changed If Not Intersect(Columns(strYN), Target) Is Nothing Then Application.ScreenUpdating = False Application.EnableEvents = False ' Find the last used row on the other sheet Set wsh = Worksheets(strOther) ' Loop through the changed cells For Each rng In Intersect(Columns(strYN), Target) ' If the value has been changed to Y... If rng.Value = "Y" Then ' Then copy the data to the next available row rng.EntireRow.Copy wsh.Range("A2").EntireRow.Insert ' And set the time stamp wsh.Range(strStamp & 2).Value = Now End If Next rng Application.CutCopyMode = False Application.EnableEvents = True Application.ScreenUpdating = True End If End Sub
kaanman
Oct 01, 2020Copper Contributor
That's working really well.
Just one edit I could use help with. On the sheet that the information is being saved too, can the script create a new row at 2 (row 1 is a header row) and then just copy the data? There for the latest information is always at the top?
HansVogelaar
Oct 01, 2020MVP
Here is a new version:
Private Sub Worksheet_Change(ByVal Target As Range)
' The other sheet where you want to store the row(s)
Const strOther = "OtherSheet"
' The column with Y/N
Const strYN = "M"
' The column for the time stamp
Const strStamp = "T"
' Variables
Dim wsh As Worksheet
Dim rng As Range
' Check whether the Y/N column has changed
If Not Intersect(Columns(strYN), Target) Is Nothing Then
Application.ScreenUpdating = False
Application.EnableEvents = False
' Find the last used row on the other sheet
Set wsh = Worksheets(strOther)
' Loop through the changed cells
For Each rng In Intersect(Columns(strYN), Target)
' If the value has been changed to Y...
If rng.Value = "Y" Then
' Then copy the data to the next available row
rng.EntireRow.Copy
wsh.Range("A2").EntireRow.Insert
' And set the time stamp
wsh.Range(strStamp & 2).Value = Now
End If
Next rng
Application.CutCopyMode = False
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End Sub- kaanmanOct 01, 2020Copper Contributor
Perfect. Thank you!!