Forum Discussion

kaanman's avatar
kaanman
Copper Contributor
Sep 30, 2020
Solved

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...
  • HansVogelaar's avatar
    HansVogelaar
    Oct 01, 2020

    kaanman 

    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

Resources