|
-
Jun 2nd, 2004, 04:57 PM
#1
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 3rd, 2004, 04:22 AM
#2
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
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...
-
Jun 3rd, 2004, 11:08 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 3rd, 2004, 11:56 AM
#4
Lively Member
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. ...
-
Jun 3rd, 2004, 07:30 PM
#5
Addicted Member
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
-
Jun 4th, 2004, 10:44 AM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 5th, 2004, 01:36 PM
#7
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:
LastCell = FindLastCell("B") 'Find the last used Cell of Column B
For X = 1 To LastCell
TempString = Cells(X, 2).Value
TempString = Right$("0000" & TempString, 4)
TempString = Left$(TempString, 2) & ":" & Right$(TempString, 2)
Cells(X, 2).NumberFormat = "hhmm"
Cells(X, 2).Value = TempString
Cells(X, 2).NumberFormat = "hh:mm"
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|