title: Using VBA to auto update note/comment in Excel
body: <p>Here is my code:</p>
  <pre><code>Private Sub Worksheet_Change(ByVal Target As Range)
      Dim c As Range
      Dim ws As Worksheet
  
      For Each c In Intersect(Target, Range(&quot;F9:N46&quot;))
          If c.Comment Is Nothing And c.Value &lt;&gt; &quot;&quot; Then
              With c.AddComment
                  .Visible = False
                  .Text Application.UserName &amp; &quot;:&quot; &amp; Date &amp; &quot; - &quot; &amp; c.Value
              End With
      
          ElseIf Not c.Comment Is Nothing And c.Value &lt;&gt; &quot;&quot; Then
              c.Comment.Text Application.UserName &amp; &quot;:&quot; &amp; Date &amp; &quot; - &quot; &amp; c.Value &amp; vbNewLine &amp; c.Comment.Text
          End If
      Next    
  End Sub
  </code></pre>
  <p>The problem is that if I edit a cell that is not in my defined range I get an error like this:</p>
  <p><a href="https://i.stack.imgur.com/zfvZU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zfvZU.jpg" alt="enter image description here" /></a></p>
  <p>How can I make this work for cell F9:N46 only?</p>