Results 1 to 7 of 7

Thread: Date/Time Conversion

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Question Date/Time Conversion

    I am importing a tab delimited file into Excel and there are a
    couple for field that are not importing correctly. The first field is a
    date field formatted as YYYYMMDD and Excel thinks it is a number.
    I need it to translate to MM/DD/YYYY. The second field is a Time
    field formatted as military time without any separators - 1600 =
    4:00 PM. What would be the simplest way to solve these issues?

    Thanks in advance.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    import them in.
    Create an extra field for each.
    Use excels formulae to convert as needed
    (value,Left, mid right functions)

    Example for the time :
    =VALUE(LEFT(B2,2)&":"&RIGHT(B2,2))
    Set the format to date / time to the style you want


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    There is no easier way, like a built in formatting function. Its just
    a different date format.

    This code would need to be put into a macro for the user to do.
    How would I reference each cell without hard coding or
    performing a loop?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    I will look at this tonight.
    This is very similar to some work we did here several weeks ago.

    I'll post a reply later today.
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  5. #5
    Addicted Member
    Join Date
    Dec 2001
    Posts
    158
    I'm a little boerd so I worked out a solution. I'm sure someone smarter could find a more quick and simple way to do this, but here's what I came up with:


    (Assuming that Column A is filled with your dates, and Column B is filled with your times, and they have the format you described in your post, I did a test with fake data and this worked)
    Code:
    Public Sub Hello()
    Dim LastCell As Integer
    Dim TempDate As Date
    Dim TempString As String
    
    LastCell = FindLastCell("A")   'Find the last used Cell of Column A
    For X = 1 To LastCell
        TempString = Cells(X, 1).Value  ' (row,column)
        TempDate = Mid$(TempString, 5, 2) & "/" & Right$(TempString, 2) & "/" & Left$(TempString, 4)
        Cells(X, 1).Value = ""
        Cells(X, 1).NumberFormat = "mm/dd/yyyy"
        Cells(X, 1).Value = TempDate
    Next X
    
    LastCell = FindLastCell("B") 'Find the last used Cell of Column B
    For X = 1 To LastCell
        TempString = Cells(X, 2).Value
        If Len(TempString) = 3 Then TempString = "0" & TempString
        TempString = Left$(TempString, 2) & ":" & Right$(TempString, 2)
        Cells(X, 2).NumberFormat = "hhmm"
        Cells(X, 2).Value = TempString
        Cells(X, 2).NumberFormat = "hh:mm AM/PM"
    
    Next X
    End Sub
    
    '-----------------------------------------------------------------------
    
    Private Function FindLastCell(MyColumn As String)
      Dim LastCell As Range
      With ActiveSheet
        Set LastCell = .Cells(.Rows.Count, MyColumn).End(xlUp)
        If IsEmpty(LastCell) Then
          'do nothing
        Else
          Set LastCell = LastCell.Offset(1, 0)
        End If
      End With
    FindLastCell = LastCell.Row - 1
    End Function

  6. #6

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Thanks Garratt, looks like this will work. I'll just need to make
    some mods on the time formatting. They want it to be military
    time (24 hours) now. I have been sooo burned out lately -
    burning the midnight oil.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Garratt, how come in the second loop for the time fis we have to
    set the .NumberFormat twice? If I set it once it doesn't work. I
    don't need the AM/PM format. We are keeping it 24 hour military
    time. So I had to do this...
    VB Code:
    1. LastCell = FindLastCell("B") 'Find the last used Cell of Column B
    2. For X = 1 To LastCell
    3.     TempString = Cells(X, 2).Value
    4.     TempString = Right$("0000" & TempString, 4)
    5.     TempString = Left$(TempString, 2) & ":" & Right$(TempString, 2)
    6.     Cells(X, 2).NumberFormat = "hhmm"
    7.     Cells(X, 2).Value = TempString
    8.     Cells(X, 2).NumberFormat = "hh:mm"
    9. Next
    Thanks
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width