Calculation in VBA of difference between TODAY and a date in another cell.

Posted on 2011-04-20
Last Modified: 2012-08-13
I have a cell with a date in it and I want to get a value that equates to the number of days from todays date.
So if the date is 5 days ago the it should return a number of -5, similarly todays date should return 0 and tomorrows date should return 1
Question by:GordonMasson
    LVL 50

    Expert Comment


    with the date in A1, try


    Make sure to format the result as General, not date!!

    cheers, teylyn
    LVL 33

    Accepted Solution

    Here's an example on how you can do this. I've putted the code on the Worksheet Change event so that when you enter a value on a cell on column A, it displays the number of days on the corresponding cell of column B.

    Please check the attached file.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cll As Range
    Dim rng As Range
    Set rng = Range("A:A")
    If Not Intersect(Target, rng) Is Nothing Then
        Target.Offset(0, 1).Value = Int(Format(Target.Value - Now(), Number))
    End If
    End Sub

    Open in new window

    LVL 85

    Expert Comment

    by:Rory Archibald
    Basically in VBA, just:
    Range("A1").value - Date

    Open in new window


    Author Closing Comment

    That works for me ...Thanks

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
    Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
    The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now