Add current date and time, to a corresponding cell, when a cell value changes
<p>I have Worksheet_Change code in my Excel worksheet. If there is a change in a given column it should add the date and time (from <code>Now()</code>) to a corresponding cell in a different column.</p>
<p>This sub should check two places and depending on which range was changed should update a different cell. If column G changes, update column A; if column K changes, update column M.</p>
<p>I'm not getting any errors. The date and time aren't being added to the corresponding cell.</p>
<p>It worked a few days ago.</p>
<pre class="lang-vb prettyprint-override"><code>Private Sub Worksheet_Change(ByVal Target As range)

Dim cell As range

'Adds unique keyA values
'Check to see if the changed cell is in column G
If Not Intersect(Target, range(&quot;G:G&quot;)) Is Nothing Then
    For Each cell In Target.Cells
        If cell.Value &lt;&gt; vbNullString And Target.Row &gt; 7 And Target.Row &lt;= 20 Then
        'Update the &quot;KeyA&quot; value
            sheets(&quot;Front End&quot;).range(&quot;A&quot; &amp; Target.Row).Value = Now()
        End If
    Next cell
Else

'Adds unique keyB values
'Check to see if the changed cell is in column K
    If Not Intersect(Target, range(&quot;K:K&quot;)) Is Nothing Then
        For Each cell In Target.Cells
            If cell.Value &lt;&gt; vbNullString And (Target.Row &gt; &quot;7&quot; And Target.Row &lt;= &quot;27&quot;) Then
            'Update the &quot;KeyM&quot; value
                sheets(&quot;Front End&quot;).range(&quot;M&quot; &amp; Target.Row).Value = Now()
            End If
        Next cell
    End If
End If
End Sub
</code></pre>
<p>The code that changes the value in row G is called by a button:</p>
<pre class="lang-vb prettyprint-override"><code>Private Sub CommandButton1_Click()
Sheets(&quot;Front End&quot;).Unprotect (&quot;29745&quot;)
h = Hour(Now)
    For Each c In range(&quot;B8:B20&quot;)
        If h = Hour(c) Then
            c.Offset(0, 3) = CInt(c.Offset(0, 3)) + 1
            Exit For
        End If
    Next c
Sheets(&quot;Front End&quot;).Protect (&quot;29745&quot;)
Unload Me
End Sub
</code></pre>