+ Reply to Thread
Results 1 to 6 of 6

Excel VBA UDF To Handle Milliseconds

Hybrid View

  1. #1
    Registered User
    Join Date
    08-31-2017
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    19

    Excel VBA UDF To Handle Milliseconds

    Hi,

    I have a DateTime String like - 24-8-2017 15:0:0:48 (Where 48 is milliseconds)

    I have written a UDF to convert it to an actual DateTImeFormat. This works just fine. However, I am unable to understand how to get the Milliseconds to be cauculated in the UDF.

    Any Help will be appreciated??

    Function DateTimeConversion(InputDate)
    
        Dim InputDate As String, DateStr, TimeStr, TimeArray
        
        DateStr = Left(InputDate, Application.WorksheetFunction.Find(" ", InputDate) - 1)
        TimeStr = Trim(Right(InputDate, Len(InputDate) - Application.WorksheetFunction.Find(" ", InputDate)))
        TimeArray = Split(TimeStr, ":")
        
        DateStr = CDate(DateStr)
        TimeStr = TimeValue((TimeArray(0) & ":" & TimeArray(1) & ":" & TimeArray(2)))
        
        
        DateTimeConversion = DateValue(DateStr) + TimeValue(TimeStr)
        
    End Function

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Excel VBA UDF To Handle Milliseconds

    This may turn out to be a silly question but why are you using Strings and Variants rather than Date/Time variables?

  3. #3
    Registered User
    Join Date
    08-31-2017
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    19

    Re: Excel VBA UDF To Handle Milliseconds

    Hi,

    Thank you for the response.

    That is because the Input is a Text String, not an actual date. And TimeArray is a Variant Array

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Excel VBA UDF To Handle Milliseconds

    This will work.
    Function DateTimeConversion(InputDate As String)
        Dim DateStr, TimeStr, TimeArray
        DateStr = Split(InputDate, " ")(0)
        TimeStr = Split(InputDate, " ")(1)
        TimeArray = Split(TimeStr, ":")
        DateStr = DateValue(DateStr)
        TimeStr = TimeValue(TimeArray(0) & ":" & TimeArray(1) & ":" & TimeArray(2)) + TimeArray(3) / 100 / 24 / 60 / 60
        DateTimeConversion = DateStr + TimeStr
    End Function
    This will return the datetime value with milliseconds included. Just use appropriate format once it's returned to cell.

    0.JPG

    NOTE: Due to VBA limitation, Debug.Print will not display milliseconds portion.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  5. #5
    Registered User
    Join Date
    08-31-2017
    Location
    India
    MS-Off Ver
    Excel 2016
    Posts
    19

    Re: Excel VBA UDF To Handle Milliseconds

    Hi,

    Thank you for the reply..

    I just got it done using this -

    Function DateTimeConversion(InputDate As String) As Date
    
        Dim DateStr As String, TimeStr As String, DateArray, TimeArray, MS As Double
        
        DateStr = Left(InputDate, Application.WorksheetFunction.Find(" ", InputDate) - 1)
        TimeStr = Trim(Right(InputDate, Len(InputDate) - Application.WorksheetFunction.Find(" ", InputDate)))
        TimeArray = Split(TimeStr, ":")
        
        If UBound(TimeArray) > 2 Then
            MS = (TimeArray(3) + 0) / 86400000
        Else
            MS = 0
        End If
        
        DateTimeConversion = DateValue(CDate(DateStr)) + TimeValue(TimeValue((TimeArray(0) & ":" & TimeArray(1) & ":" & TimeArray(2)))) + MS
        
    End Function
    Thank You. Rep added
    Last edited by DD646; 08-31-2017 at 08:23 AM.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Excel VBA UDF To Handle Milliseconds

    Thanks for the rep and sharing solution found

    Please mark the thread as solved by using thread tool found at top of your initial post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Tab Key handle in excel VBA
    By sanguapkar in forum Excel General
    Replies: 0
    Last Post: 12-12-2012, 07:32 AM
  2. [SOLVED] Excel function to find difference of timestamps in milliseconds?
    By Srikanth in forum Excel General
    Replies: 1
    Last Post: 07-06-2006, 03:10 AM
  3. Can excel handle more than 256 columns?
    By Nodles in forum Excel General
    Replies: 1
    Last Post: 12-06-2005, 07:35 AM
  4. [SOLVED] Excel should handle more than 65,536 rows.
    By colekp in forum Excel General
    Replies: 2
    Last Post: 09-26-2005, 11:05 AM
  5. To many rows for EXCEL to handle
    By moglione1 in forum Excel General
    Replies: 3
    Last Post: 09-09-2005, 10:05 AM
  6. Too much to handle for Excel?
    By Bart Snel in forum Excel General
    Replies: 3
    Last Post: 05-19-2005, 09:06 AM
  7. [SOLVED] Milliseconds in Excel
    By dhg4 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-12-2005, 04:06 PM
  8. Replies: 1
    Last Post: 02-20-2005, 10:06 AM

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