+ Reply to Thread
Results 1 to 9 of 9

Using macro to input static date

Hybrid View

  1. #1
    Registered User
    Join Date
    06-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Using macro to input static date

    Hi,

    I found a piece of code on the internet that input the date into one column upon changing a another; however, I wanted to alter the code so that it would only input the date upon something being entered into the target column and not just any change (at the moment it puts the date in even if I clear all the information). I was also wondering whether it was possible to have the date disappear if the information is deleted.

    Here is the current code. I'd appreciate all the help I can get:


    Private Sub Worksheet_Change(ByVal Target As Range)
    Set t = Target
    Set a = Range("D:D")
    If Intersect(t, a) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    t.Offset(0, 9).Value = Date
    Application.EnableEvents = True
    End Sub
    Last edited by Dark Pelican; 06-29-2011 at 06:44 AM.

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Using macro to input static date

    As per forum rules, your code should be inside code html for ease of reading.

    http://www.excelforum.com/forum-rule...rum-rules.html

    Try this code
    Private Sub Worksheet_Change(ByVal Target As Range)
    Set t = Target
    Set a = Range("D:D")
    If Intersect(t, a) Is Nothing Then Exit Sub
        Application.EnableEvents = False
        If t.Value = "" Then
            t.Offset(0, 9).ClearContents
        Else
            t.Offset(0, 9).Value = Date
    Application.EnableEvents = True
    End If
    
    End Sub
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    06-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Using macro to input static date

    Thanks for your help, but unfortunately it doesn't work. It does the job initially, as it enters the date in the offset field upon inputting data into column D, and then clears the date if the data is removed; however, after the first time you clear a cell in column D, any further entries do not result in the date being input into the offset column. It is also only possible to have one offset cell cleared, as after the first 'delete', it stops clearing the other offset fields in response to further deletions on data from column D.

    Thanks for trying though, it was partially successful.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,426

    Re: Using macro to input static date

    Try:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Set t = Target
    Set a = Range("D:D")
    If Intersect(t, a) Is Nothing Then Exit Sub
    Application.EnableEvents = False
        If t.Value = "" Then
            t.Offset(0, 9).ClearContents
        Else
            t.Offset(0, 9).Value = Date
        End If
    Application.EnableEvents = True
    
    End Sub


    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Registered User
    Join Date
    06-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Using macro to input static date

    Thanks TM Shucks, it works perfectly.
    Last edited by Dark Pelican; 06-29-2011 at 08:02 AM.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,426

    Re: Using macro to input static date

    You're welcome ... but the credit should go to ChemistB, it's his code. I just moved a line.

    Thanks for the rep.

    If this has answered your question, please mark your thread as Solved.

    Regards

  7. #7
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Using macro to input static date

    Oops, my bad. Thanks for the fix TM.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,426

    Re: Using macro to input static date

    @ChemistB: no problem, easily done. TMS

  9. #9
    Registered User
    Join Date
    06-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Using macro to input static date

    Anyway, I've pressed the scales for both of you, so thanks.

+ 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