Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:03:41+00:00 2026-06-04T17:03:41+00:00

I’m trying to use the DateDiff function for a macro in excel using VBA.

  • 0

I’m trying to use the DateDiff function for a macro in excel using VBA.

To my understanding the code is supposed to return an integer value.

Here are the examples given online at this location: http://www.techonthenet.com/access/functions/date/datediff.php

-DateDiff (“yyyy”, #15/10/1998#, #22/11/2003#) would return 5

-DateDiff (“m”, #15/10/2003#, #22/11/2003#) would return 1

-DateDiff (“d”, #15/10/2003#, #22/11/2003#) would return 38

Here is the code that I used:

Sub macrotest()

Dim dateNow, dateThen, dateFinal As Date

dateNow = Format(Sheet1.Cells(2, 2), "DD/MM/YY")
dateThen = Format(Sheet1.Cells(3, 2), "DD/MM/YY")
dateFinal = DateDiff("d", dateNow, dateThen)

Sheet1.Cells(5, 2) = dateFinal


End Sub

Lastly, This is what I had in my excel rows:

B2: 5/30/12

B3: 3/30/12

B5: 12:00:00 AM (this cell was supposed to have the result listed).

-I got either dates that excel defaults to, or 12:00.

Any Ideas? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:03:43+00:00Added an answer on June 4, 2026 at 5:03 pm

    According to the help system, DateDiff returns a Long.

    Your code declares two Variants and one Date type. Instead of

    Dim dateNow, dateThen, dateFinal As Date

    you probably meant

    Dim dateNow As Date, dateThen As Date, dateFinal As Long

    This code worked for me. You may need to tweak it slightly to apply it to your particular situation (I used ActiveSheet to make the testing go faster)

    Sub macrotest()
    
    Dim dateNow As Date, dateThen As Date, dateFinal As Long
    
    dateNow = ActiveSheet.Cells(2, 2).value
    dateThen = ActiveSheet.Cells(3, 2).value
    dateFinal = DateDiff("d", dateThen, dateNow)
    
    ActiveSheet.Cells(5, 2) = dateFinal
    
    End Sub
    

    Also I believe you have dateNow and dateThen reversed in your DateDiff function call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.