vba - Excel automatically add comment with cell edit history -
i have following code in "sheet macros" (right click sheet - view code). used work it's not adding comments in specified range a5:aq155.
private sub worksheet_change(byval target range) application.enableevents = false 'if (target.row > 3 , target.row < 155) cells(target.row, "at") = now() const srng string = "a5:aq155" ' change required dim sold string dim snew string dim scmt string dim ilen long dim bhascomment boolean target(1) if intersect(.cells, range(srng)) nothing exit sub snew = .text sold = .text .value = snew application.enableevents = true scmt = "edit: " & format$(now, "dd mmm yyyy hh:nn:ss") & " " & application.username & chr(10) & "previous text :- " & sold if target(1).comment nothing .addcomment else ilen = len(.comment.shape.textframe.characters.text) end if .comment.shape.textframe .autosize = true .characters(start:=ilen + 1).insert iif(ilen, vblf, "") & scmt end end end sub
what have done wrong?
the code stopped firing because event firing disabled , never turned on. way code written, makes change worksheet outside range a5:aq155
, events become disabled without being turned on, means subsequent event triggers not fired (ie. - next time edit cell).
if make these slight tweaks in code should work intended going forward.
however, before type application.enableevents = true
in immediate window , hit enter turn events on code begins fire again.
private sub worksheet_change(byval target range) const srng string = "a5:aq155" ' change required dim sold string dim snew string dim scmt string dim ilen long if not intersect(target, me.range(srng)) nothing application.enableevents = false target snew = .value2 application.undo sold = .value2 .value2 = snew application.enableevents = true scmt = "edit: " & format$(now, "dd mmm yyyy hh:nn:ss") & " " & application.username & chr(10) & "previous text :- " & sold if .comment nothing .addcomment else ilen = len(.comment.shape.textframe.characters.text) end if .comment.shape.textframe .autosize = true .characters(start:=ilen + 1).insert iif(ilen, vblf, "") & scmt end end end if end sub
Comments
Post a Comment