Consulting

Results 1 to 6 of 6

Thread: subtract today's date from a date in another column

  1. #1
    VBAX Contributor
    Joined
    Jun 2019
    Posts
    155
    Location

    subtract today's date from a date in another column

    Dear Team,

    I am having dates in Worksheet Sheet1 column D. I want to subtract Todays date from a date in column D and update on Column L.

    For that i am trying the following code. But it is not working

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim LastRow As Long, i As Long
     With Worksheets("Sheet1")
     LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
     For i = 2 To LastRow
     .Range("L" & i).Value = Date - Range("D" & i).Value 
     Next i
     End With
    End Sub



    Can any one tell me what is the mistake on above code

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Target.Column <> 4 Then Exit Sub
        
        With Me.Cells(Target.Row, "L")
        
            .Value = Date - Target.Value
            .NumberFormat = "General"
        End With
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Contributor
    Joined
    Jun 2019
    Posts
    155
    Location
    Dear XLD,

    Thanks for your reply.

    When is trying to upload my datas and using the above your code i am getting "Type Mismatch" error.

    I attached my files here for your kind reference.

    Kindly check and update
    Attached Files Attached Files

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Can any one tell me what is the mistake on above code
    Probably the missing Dot before Range. Try this
    .Cells(i, "L") = DateDiff("d", Date, CDate(.Range("D" & i)))
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Little more elaborate

    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r As Range
    
    
        Application.EnableEvents = False
    
    
        For Each r In Intersect(Target, Me.UsedRange).Cells
            With r
                If .Column <> 4 Then GoTo NextCell
                If Not IsDate(.Value) Then GoTo NextCell
    
    
                .Offset(0, 8).Value = Date - .Value
                .Offset(0, 8).NumberFormat = Application.International(xlGeneralFormatName)
            End With
    NextCell:
        Next
    
    
        Application.EnableEvents = True
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    i have written

    =Today() in cell A1.
    anydate in cell B1
    Then A1-B1 in cell C1 and changed it to datatype General

Posting Permissions

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