0

am having one Excel file it contains the data with multiple sheets. In that One column having time stamp like 21/6/12 10:33:07:AM. My system date format is "m/d/yyyy"
while converting this to Date 21/06/2012. am able to get the date but it is showing in wrong. 06-12-2021 this should be 21-06-2012. am using the below code.

Ex : 26/6/12 11:15:07:AM should be 26/06/2012
21/6/12 10:33:07:AM should be 21/06/2012

Public Sub ConvtDate()
Dim ParseDateTime As Date
Application.ScreenUpdating = False
For Each datcol In ws_Raw2.Range("I2:I65536")  
x = InStr(1, datcol, " ", vbTextCompare) - 1  
If x > 0 Then  
    ParseDateTime = DateValue(Left(datcol, x))  
    datcol.Value = ParseDateTime
End If  
Next  
Application.ScreenUpdating = True  
End Sub  

Please help how to get the date.
Thanks in advance.

2 Answers 2

3

DateValue expects a date as a string in the format set by your system.
From Excel Help:

If date is a string that includes only numbers separated by valid date separators, DateValue recognizes the order for month, day, and year according to the Short Date format you specified for your system.

Since your data is in Day Month Year and your system is Month Day Year you will need to reconstruct the parameter you pass to DateValue.

Here's a refactor of your coed, also dealing with a few other issues:

  • Dim all your variables (use Option Explicit to force this)
  • Only process rows you need to
  • Loop a variant array rather than a range, it much faster

Public Sub ConvtDate()
    Dim ParseDateTime As Date
    Dim i As Long
    Dim x As Long
    Dim a() As String
    Dim dat As Variant
    Dim rng As Range

    Application.ScreenUpdating = False
    Set rng = Range(Cells(2, 9), Cells(Rows.Count, 9).End(xlUp))
    dat = rng.Value
    For i = 1 To UBound(dat, 1)
        x = InStr(1, dat(i, 1), " ", vbTextCompare) - 1
        If x > 0 Then
            a = Split(Left(dat(i, 1), x), "/")
            ParseDateTime = DateValue(a(1) & "/" & a(0) & "/" & a(2))
            ' or if you dont know the system data format use
            ' ParseDateTime = DateSerial(a(2), a(1), a(0))
            dat(i, 1) = ParseDateTime
        End If
    Next
    rng = dat
    Application.ScreenUpdating = True
End Sub
6
  • Thanks Chris. But the code is looping (iterating) only one time Aug 26, 2013 at 9:37
  • For i = 1 To UBound(dat, 2) can you please help how to resolve this. Aug 26, 2013 at 9:43
  • Oops, a typo... should be For i = 1 To UBound(dat, 1) Aug 26, 2013 at 9:51
  • Thanks a lot Chris, it worked like a charm... if we dont know the system date format, is there anyway to do this ? is the only way to identify the system date format and do the formating Aug 26, 2013 at 10:01
  • You couls use ParseDateTime = DateSerial(a(2), a(1), a(0)) instead Aug 26, 2013 at 10:04
0

Excel will change 26/6/12 to 26/06/2012 itself if the column formating ins date. So

ParseDateTime = Left(datcol, x)

should do it.

1
  • But it is changing to 12/06/2026. instead of 06/26/2012 Aug 26, 2013 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.