EXCEL VBA Date format question

Posted on 2007-08-08
Last Modified: 2013-12-25
I am working in Excel VBA and am trying to convert a string to date format and then calculate the number of days between the newdate (strdate formatted to Date type) and Todays date which is stored in my Excel spreadsheet.  I am getting a type mismatch error on the following line of code:

 newdate = "=DATE(strdate)"

The entire subroutine is listed below:

Sub fmtdate(ByVal agedate)
Dim newdate As Date
Dim strdate As String
Dim length As Integer
TodaysDate = ActiveSheet.Range("E2")
length = Len(Trim(agedate))

Select Case length
    Case Is = 5
        strdate = Right(agedate, 2) & "/0" & (Left(agedate, 1)) & "/" _
         & Mid(agedate, 2, 2)
          newdate = "=DATE(strdate)"
       ActiveCell.Offset(0, 4).FormulaR1C1 = "=DAYS360(newdate,TodaysDate)"
    Case Is = 6
        strdate = Right(agedate, 2) & "/" & (Left(agedate, 2)) & "/" _
       & Mid(agedate, 3, 2)
         newdate = "=DATE(strdate)"
        ActiveCell.Offset(0, 4).FormulaR1C1 = "=DAYS360(newdate,TodaysDate)"
End Select      
End Sub

Help would be greatly appreciated!!
Question by:Jeanniem
    LVL 9

    Accepted Solution


    Try newdate = DateValue(strdate)

    LVL 38

    Assisted Solution

    Hi Jeanniem,

    Try changing this:

    newdate = "=DATE(strdate)"

    to this:

    newdate = CVDate(strdate)


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 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

    22 Experts available now in Live!

    Get 1:1 Help Now