Consulting

Results 1 to 11 of 11

Thread: Solved: put current date in a column if any change in row

  1. #1

    Solved: put current date in a column if any change in row

    A newbie to excel and wondering how to do this. I have an excel sheet with say data in column A to F. What I want is if I change the value of any column from A to F, column H should be set to current date automatically.

    Any help much appreciated

    Sreejith

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Sreejth,

    Welcome to vbaexpress!

    Are you asking for help with a macro/vba code? Presuming yes for the moment, do you mean that if any of the cells' values in a given row (from columns A - F) are changed by the user (vs. calculation) would result in the date being updated in H(whatever row)?

    Mark

  3. #3
    Mark,
    Thanks. Yes that's what I am looking for.

    I got a piece of code from this site which will update the comment and tried to hack it. But this fills in column H of all the rows. How do I just update the row I changed.

    Option Explicit
    Public preValue As Variant
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    Columns("H") = "Previous Value was " & preValue & Chr(10) & "Revised " & Format(Date, "mm-dd-yyyy") & Chr(10) & "By " & Environ("UserName")
    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target = "" Then
    preValue = "a blank"
    Else: preValue = Target.Value
    End If
    End Sub

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Try this

    [vba]
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim H as Long

    H = 8 'Column number of "H"
    If Target.Count > 1 Then Exit Sub
    Cells(Target.Row, H) = Now
    End Sub

    [/vba]

    You'll have to put in some checks to make sure that "Target" is within the range of cells you're watching.

    SamT

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Adding a few Checks to Sam's code:

    [VBA]Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim H As Long
    H = 8 'Column number of "H"
    If Target.Count > 1 Then Exit Sub 'if more than one cell selected
    If Target.Row = 1 Then Exit Sub 'do nothing on header row
    If Target.Column >= 1 And Target.Column <= 6 Then ' if in column A -F then add date
    Cells(Target.Row, H) = Now
    Else
    Exit Sub
    End If
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Sam/lucas
    Thanks very much for your help. Worked perfectly

    Thanks
    Sreejith

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Sreejith, be sure to mark your thread solved using the thread tools at the top of the page.

    That way others won't read the entire thread to find that it's been solved.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Lucas,
    Thanks for the reminder.

    sreejith

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Thank you Lucas.

    I had just gotten that little bit tested when SWMBO said, "We're leaving."

    Yipes!

    To make it a little more self documenting:
    [vba]
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Date_Column As Long
    Date_Column = 8 'Column number of "H"
    Dim Header_Row As Long
    Header_Row = 1 'Number of bottom row of headers

    If Target.Count > 1 Then Exit Sub 'if more than one cell selected
    If Target.Row <= Header_Row Then Exit Sub 'do nothing on header row
    If Target.Column >= 1 And Target.Column <= 6 Then ' if in column A -F then add date
    Cells(Target.Row, Date_Column) = Now
    Else
    Exit Sub
    End If
    End Sub

    [/VBA]
    SamT

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    No wonder you were scrambling.

    Nice add on the code too. Makes Multiple header rows easy to deal with too.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I always want to be able to come back in a year and understand what's going on at first reading.

    SamT

Posting Permissions

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