+ Reply to Thread
Results 1 to 7 of 7

Help with Date & Time Stamp (Comments)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-13-2013
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    14

    VBA for comments Time and Date Stamp etc. - Multiple cells in one column

    Hi,

    Apologies if the answer is somewhere already on here (being an amateur user of Excel, i'm bungling along and learning as I go!), I have the following code;

    Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
    If .Address <> "$A$1" Then Exit Sub
    If IsEmpty(Target) Then Exit Sub
    Dim strNewText$, strCommentOld$, strCommentNew$
    strNewText = .Text
    If Not .Comment Is Nothing Then
    strCommentOld = .Comment.Text & Chr(10) & Chr(10)
    Else
    strCommentOld = ""
    End If
    On Error Resume Next
    .Comment.Delete
    Err.Clear
    .AddComment
    .Comment.Visible = False
    .Comment.Text Text:=strCommentOld & _
    Format(VBA.Now, "MM/DD/YYYY at h:MM AM/PM") & Chr(10) & strNewText
    .Comment.Shape.TextFrame.AutoSize = True
    End With
    End Sub

    Now, the problem is that I wish to extend the code to work in multiple cells contained within one column i.e. say from H1 to H99, but for the life of me I cannot seem to work a way to achieve the result I desire - can doctor the code and make it work in every cell in every column, but this isn't ideal as I really want to limit the operation of the "comments/date and time/changes" limited to one column, multiple cell only!.

    Many thanks for reading!

    Last edited by arlu1201; 06-17-2013 at 07:16 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: VBA for comments Time and Date Stamp etc. - Multiple cells in one column

    Replace

    If .Address <> "$A$1" Then Exit Sub

    with (for H1:H99)

    If .column <> 8 then exit sub
    If .row < 100 then exit sub

    If you understand the technique it's easily modified
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    06-13-2013
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    14

    Re: VBA for comments Time and Date Stamp etc. - Multiple cells in one column

    Hi Andy,

    Many thanks for the reply.......replaced the line "If .Address <> "$A$1" Then Exit Sub" with the "If .column <> 8 then exit sub
    If .row < 100 then exit sub"


    so the coding now is;

    Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
    If .Column <> 8 Then Exit Sub
    If .Row < 100 Then Exit Sub
    If IsEmpty(Target) Then Exit Sub
    Dim strNewText$, strCommentOld$, strCommentNew$
    strNewText = .Text
    If Not .Comment Is Nothing Then
    strCommentOld = .Comment.Text & Chr(10) & Chr(10)
    Else
    strCommentOld = ""
    End If
    On Error Resume Next
    .Comment.Delete
    Err.Clear
    .AddComment
    .Comment.Visible = False
    .Comment.Text Text:=strCommentOld & _
    Format(VBA.Now, "MM/DD/YYYY at h:MM AM/PM") & Chr(10) & strNewText
    .Comment.Shape.TextFrame.AutoSize = True
    End With
    End Sub
    Unfortunately it didn't work, so not sure what to do...........

    Apologies, i'm a real amateur with these things, don't understand coding at all and trying to get this sorted for a work project which I've been tasked.........
    Last edited by arlu1201; 06-17-2013 at 07:16 AM.

  4. #4
    Registered User
    Join Date
    06-13-2013
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    14

    Help with Date & Time Stamp (Comments)

    Hi,

    Apologies for re-posting......just hoping someone has the answer - a forum member very kindly posted a reply last week to a question that I posed regarding a problem i'm having with some code (unfortunately the solution didn't work). I'm trying to achieve a date and time stamp log of all entries within a specific column and range, and thus far I cannot achieve any result other than the code below working in every single cell on the spreadsheet. Ideally i'm looking to isolate the code to work in column H and from row 1-100.


    Private Sub Worksheet_Change(ByVal Target As Range)
     With Target
     If .Column <> 8 Then Exit Sub
     If .Row < 100 Then Exit Sub
     If IsEmpty(Target) Then Exit Sub
     Dim strNewText$, strCommentOld$, strCommentNew$
     strNewText = .Text
     If Not .Comment Is Nothing Then
     strCommentOld = .Comment.Text & Chr(10) & Chr(10)
     Else
     strCommentOld = ""
     End If
     On Error Resume Next
     .Comment.Delete
     Err.Clear
     .AddComment
     .Comment.Visible = False
     .Comment.Text Text:=strCommentOld & _
     Format(VBA.Now, "MM/DD/YYYY at h:MM AM/PM") & Chr(10) & strNewText
     .Comment.Shape.TextFrame.AutoSize = True
     End With
     End Sub

    Any ideas greatly appreciated!

    Thanks
    Last edited by arlu1201; 06-17-2013 at 07:15 AM.

  5. #5
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Help with Date & Time Stamp (Comments)

    I removed the 'with' statements, cause that made it not work for me.
    Code below works fine.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column <> 8 Or Target.Row > 100 Then Exit Sub
    If IsEmpty(Target) Then Exit Sub
    Dim nt$, co$, cn$
    nt = Target.Text
    If Not Target.Comment Is Nothing Then
    co = Target.Comment.Text & Chr(10) & Chr(10)
    Else
    co = ""
    End If
    On Error Resume Next
    Target.Comment.Delete
    Err.Clear
    Target.AddComment
    Target.Comment.Visible = False
    Target.Comment.Text Text:=co & Format(VBA.Now, "MM/DD/YYYY at h:MM AM/PM") & Chr(10) & nt
    Target.Comment.Shape.TextFrame.AutoSize = True
    End Sub
    Please click the * below if this helps

  6. #6
    Registered User
    Join Date
    06-13-2013
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    14

    Re: Help with Date & Time Stamp (Comments)

    Jasper....many thanks for that....works like a charm!

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Help with Date & Time Stamp (Comments)

    First.Officer,

    Its against the forum rules to post duplicate threads. I have merged both your threads.

    Also,

    I have added code tags to your posts. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. In order to put code tags, either type [CODE] before your code and [/CODE] at the end of it, OR you can highlight your code and click the # icon at the top of your post window.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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