Convert HHMMSS to time in VBA?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
616
Hi,
In a database our timevalues are stored like
94500 = 09:45:00
121755 = 12:17:55
58 = 00:00:58

What is the best way to convert these when pulling them via ODBC? Ideally in the SQL query but power query is an option also.

Thanks
 

joeu2004

Well-known Member
Joined
Mar 2, 2014
Messages
2,580
Office Version
2010
Platform
Windows
Rich (BB code):
Sub doit()
Dim x As Long
x = 94500
MsgBox Format(x, "00\:00\:00")
x = 121755
MsgBox Format(x, "00\:00\:00")
x = 58
MsgBox Format(x, "00\:00\:00")
End Sub
The type of "x" does not matter (Long, Double, Variant), as long as it can handle large numbers (i.e. not Integer).
 
Last edited:

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
616
Rich (BB code):
Sub doit()
Dim x As Long
x = 94500
MsgBox Format(x, "00\:00\:00")
x = 121755
MsgBox Format(x, "00\:00\:00")
x = 58
MsgBox Format(x, "00\:00\:00")
End Sub
The type of "x" does not matter (Long, Double, Variant), as long as it can handle large numbers (i.e. not Integer).
thankyou,
i put this together and its working

Rich (BB code):
Sub TimeConvert()
    
    Line = 2
    
    Do Until Cells(Line, 1).Value = ""
        Cells(Line, 1).Value = Format(Cells(Line, 1).Value, "00\:00\:00")
        Line = Line + 1
    Loop


End Sub
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,504
Office Version
2010
Platform
Windows
i put this together and its working

Code:
Sub TimeConvert()
    
    Line = 2
    
    Do Until Cells(Line, 1).Value = ""
        Cells(Line, 1).Value = Format(Cells(Line, 1).Value, "00\:00\:00")
        Line = Line + 1
    Loop

End Sub
You could also do it without using a loop like this...
Code:
Sub TimeConvert()
  Dim Addr As String
  Addr = "A2:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF(@="""","""",TEXT(@,""00\:00\:00""))", "@", Addr))
End Sub
 

Forum statistics

Threads
1,081,973
Messages
5,362,505
Members
400,677
Latest member
champchamp

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top