How do I convert a given number into time format HH:MM:SS in VBA?

前端 未结 3 855
感动是毒
感动是毒 2020-12-22 03:52

I received a list of numbers in Custom format (Type: 000000) that represent military time. It always contain 6 digits, and leading zeros are used when necessary. For examp

3条回答
  •  既然无缘
    2020-12-22 04:35

    I can't tell if you want VBA code, or a formula for a cell, or what Custom, type 00000 is. Sean's answer above is probably the best from the standpoint of using VBA functions properly, but I don't believe there's any way to format a time object in military time with "(am)" and "(pm)" tacked on to the end. At least not using standard Excel formatting. (Probably because am and pm are redundant in military time.)

    So assuming your source data are really strings, here's an alternative, in case you really want 'am' and 'pm' for some reason:

    =MID(A1,1,2) & ":" & MID(A1,3,2) & ":" & MID(A1,5,2) & IF(VALUE(MID(A1,1,2)) < 12," (am)", " (pm)")
    

提交回复
热议问题