VBA code not working in a Range

GSY01

New Member
Joined
Sep 18, 2019
Messages
8
Hi Guys,

I have the following code to add a comment each time a cell value changes, however if I change the Range to "N25" everything works perfectly, but if I want to have the code to cover all rows in column "N" I don't get any comment appear when the value changes.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range
Set Rng = Range("N:N")
On Error Resume Next
If Rng.Comment Is Nothing And Len(Rng.Value) > 0 Then
Rng.AddComment Now & "-" & Rng.Value
Else
Rng.Comment.Text vbNewLine & Now & "-" & Rng.Value, Len(Rng.Comment.Text) + 1
End If
Rng.Comment.Shape.TextFrame.AutoSize = True
End Sub

Thanks,

Gavin
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range

    Set Rng = Range("N:N")

    If Intersect(Target, Rng) Is Nothing Then Exit Sub

    On Error Resume Next

    If Target.Comment Is Nothing And Len(Target.Value) > 0 Then 
        Target.AddComment Now & "-" & Target.Value
    Else
        Target.Comment.Text vbNewLine & Now & "-" & Target.Value, Len(Target.Comment.Text) + 1
    End If

    Target.Comment.Shape.TextFrame.AutoSize = True

End Sub
 
Upvote 0
Hi Norie,

I apologize I should have mentioned earlier, I have the following formula in column "N" =INDEX(A$1:M$1,MATCH(TRUE,(T25:AF25<>0)*(COLUMN(T25:AF25)-COLUMN(T25)+1)>=D25,0))

If I use the code I have and only select one cell in "N", it works, but not when I select all on "N", if I type over the formula using my code I still don't get a comment.

When I use your code the only time it returns a comment is when I type over the formula.

is there away I can get the comment by leaving the formula in place.

Thanks
 
Upvote 0
The change event is only triggered when a manual change is made on the worksheet.
 
Upvote 0
Excuse my ignorance as I am new to VBA, why does the code work if I only have the range as one cell i.e. "N25" I change the information in cell "D25" which changes the information in "N25" and inserts a comment.
As soon as I change the range to anything other than a single cell no comment is inserted.
 
Upvote 0
Gavin

Your original code would be triggered by a manual change in any cell on the worksheet.

The code itself doesn't actually check, or do anything, with the cell/range that has been changed but it does look at/work with column N.

Why it only changes N25 I'm not sure, do any other cells in column N have comments?
 
Upvote 0
Hi Norie,

No Other comments in N.

I will work on a different outcome.

Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,213,530
Messages
6,114,162
Members
448,554
Latest member
Gleisner2

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