+ Reply to Thread
Results 1 to 5 of 5

Change event to Add or update comment with input box when cell in a range is changed

Hybrid View

fratello Change event to Add or update... 08-27-2012, 05:50 AM
Leith Ross Re: Change event to Add or... 08-27-2012, 12:06 PM
fratello Re: Change event to Add or... 08-27-2012, 08:47 PM
Leith Ross Re: Change event to Add or... 08-29-2012, 04:40 AM
fratello Re: Change event to Add or... 08-29-2012, 06:40 AM
  1. #1
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Change event to Add or update comment with input box when cell in a range is changed

    Hi VBA experts,
    I know there are many existing codes on updating comments online, but i am just not fit enough to change those macros to suit my needs. What i am looking for is something similar to this code below (found here: http://www.mrexcel.com/forum/excel-q...t-comment.html), but with multiple ranges such as A:B, D:G, T:Z

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim oComment As comment, cell As Range, strPrev As String
    
        If Not Intersect(Target, Columns("A:B")) Is Nothing Then
    
            For Each cell In Intersect(Target, Columns("A:B"))
    
                Set oComment = Nothing
                On Error Resume Next
                    Set oComment = cell.comment
                On Error GoTo 0
    
                If Not oComment Is Nothing Then
                    strPrev = Mid(oComment.Text, InStr(oComment.Text, "Current value: ") + 15, 999)
                    oComment.Text Text:="Previous value: " & strPrev & Chr(10) & _
                                        "Changed: " & Format(Now, "mmm dd, yyyy  h:mm AM/PM") & Chr(10) & _
                                        "Current value: " & cell.Value
                                        
                ElseIf Not IsEmpty(cell) Then
                    cell.AddComment
                    cell.comment.Text Text:="Previous value: Empty" & Chr(10) & _
                                            "Changed: " & Format(Now, "mmm dd, yyyy  h:mm AM/PM") & Chr(10) & _
                                            "Current value: " & cell.Value
                    cell.comment.Shape.Width = 150
                    cell.comment.Shape.Height = 35
                    
                End If
            Next cell
        End If
       
    End Sub
    However, what I would additionally need is an input box, which enables me to enter the comment (without deleting previous comments) when a cell is changed. So in the end, each comment entered into a specific cell in this range would be in the cell that was changed along with the date and time when the comment was edited/created. Is that possible?
    Hope you understand what I mean

    Thanks a lot

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Change event to Add or update comment with input box when cell in a range is changed

    Hello fratello,

    Here is the amended version of the macro. Let me know the results.
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim oComment As Comment, cell As Range, Rng As Range
       Dim Text As String, OldText As String
       
        If Intersect(Target, Range("A:B,D:G,T:Z")) Is Nothing Then Exit Sub
    
        Text = InputBox("Please enter your comment below. Click OK when you are done.")
        
        For Each Rng In Target.Areas
            For Each cell In Rng
    
                Set oComment = cell.Comment
    
                If oComment Is Nothing Then
                    Set oComment = cell.AddComment
                    oComment.Shape.TextFrame.Characters.Text = "Entered: " & Format(Now, "mmm dd, yyyy  h:mm AM/PM") & "   " & Text & vbCrLf _
                                                             & "Value: " & Target & vbCrLf
                    oComment.Shape.TextFrame.AutoSize = True
                Else
                    OldText = oComment.Text
                    oComment.Shape.TextFrame.Characters.Text = OldText & "Changed: " & Format(Now, "mmm dd, yyyy  h:mm AM/PM") & "   " & Text & vbCrLf _
                                                             & "Value: " & Target & vbCrLf
                End If
                
            Next cell
        Next Rng
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Change event to Add or update comment with input box when cell in a range is changed

    Good Morning Leith Ross
    Thanks a lot for your post. This works perfectly fine, unless there are more than 5 changes to a cell. *Edit: I guess there is some kind of restriction based on the number of letters in the comment box.
    Only the first 5 changes are shown. Is their a way to show the last newest 5 commentaries? Or a way to copy every comment that has been made to a seperate worksheet, so that there is a history?
    Thanks a lot
    Last edited by Cutter; 08-28-2012 at 08:55 AM. Reason: Removed whole post quote

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Change event to Add or update comment with input box when cell in a range is changed

    Hello fratello,

    The problem with the comment was it accepts only 255 characters when the string is assigned by the text property. By using the Insert method, the 255 character limit no longer applies. I made a few changes to the macro to address the problem. Let me know the results on your machine.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    ' Written: August 27, 2012
    ' Updated: August 26, 2012 - Comment is no longer limited to 255 characters
    ' Author:  Leith Ross
    ' Summary: Tracks the changes made to a cell. A timestamp is added along with the value for each change.
    
       Dim oComment As Comment, Cell As Range, Rng As Range
       Dim Text As String, Newtext As String, OldText As String
       
        If Intersect(Target, Range("A:B,D:G,T:Z")) Is Nothing Then Exit Sub
    
        Text = InputBox("Please enter your comment below. Click OK when you are done.")
        
        For Each Rng In Target.Areas
            For Each Cell In Rng.Cells
    
                Set oComment = Cell.Comment
    
                If oComment Is Nothing Then
                    Set oComment = Cell.AddComment
                    oComment.Shape.TextFrame.Characters.Text = "Entered: " & Format(Now, "mmm dd, yyyy  h:mm AM/PM") & "   " _
                                                             & Text & vbCrLf & "Value: " & Target & vbCrLf
                    oComment.Shape.TextFrame.AutoSize = True
                Else
                    OldText = oComment.Text
                    Newtext = "Changed: " & Format(Now, "mmm dd, yyyy  h:mm AM/PM") & "   " _
                            & Text & vbCrLf & "Value: " & Target & vbCrLf
                    oComment.Shape.TextFrame.Characters(Len(OldText) + 1, Len(Newtext)).Insert Newtext
                End If
                
            Next Cell
        Next Rng
        
    End Sub

  5. #5
    Registered User
    Join Date
    07-27-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Change event to Add or update comment with input box when cell in a range is changed

    HI Leith,
    Thank u very much. this works perfectly.
    A minor detail would be the order of the comments, starting with the newest one and ending with the oldest one. But it is not that important.
    Thank u very much

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1