Time Format Nightmare Formula

Devin

Board Regular
Joined
Jan 21, 2009
Messages
105
I need to convert these funky time formats into a time format that excel will recognize. Additionally, not all the cells this formula points to are times so it must return "". I managed to get the job done with the UDFs and formula shown below. Does anyone have a better idea?

UDFs:
Function HourFormat(ByRef Hf As Range)
HourFormat = Evaluate("IF(LEN(" & Hf.Address & ")>4,LEFT(" & Hf.Address & ",2),LEFT(" & Hf.Address & ",1))")
End Function
Function MinFormat(ByRef Mf As Range)
MinFormat = Evaluate(Mid(Right(Mf, 3), 1, 2))
End Function

Formula:

=IF(OR(RIGHT(A2)="a",RIGHT(A2)="p"),TRIM(IF(ISERROR(Hourformat(A2)+MinFormat(A2)),"",Hourformat(A2)&":"&IF(MinFormat(A2)<10,"0"&MinFormat(A2),MinFormat(A2))&IF(RIGHT(A2)="a"," AM"," PM"))),"")

Sheet1

<TABLE style="PADDING-RIGHT: 2pt; PADDING-LEFT: 2pt; FONT-SIZE: 11pt; FONT-FAMILY: Calibri,Arial; BACKGROUND-COLOR: #ffffff" cellSpacing=0 cellPadding=0 border=1><COLGROUP><COL style="FONT-WEIGHT: bold; WIDTH: 30px"><COL style="WIDTH: 64px"><COL style="WIDTH: 64px"></COLGROUP><TBODY><TR style="FONT-WEIGHT: bold; FONT-SIZE: 8pt; BACKGROUND-COLOR: #cacaca; TEXT-ALIGN: center"><TD></TD><TD>A</TD><TD>B</TD></TR><TR style="HEIGHT: 18px"><TD style="FONT-SIZE: 8pt; BACKGROUND-COLOR: #cacaca; TEXT-ALIGN: center">1</TD><TD>1000a</TD><TD style="TEXT-ALIGN: right">10:00 AM</TD></TR><TR style="HEIGHT: 18px"><TD style="FONT-SIZE: 8pt; BACKGROUND-COLOR: #cacaca; TEXT-ALIGN: center">2</TD><TD>1100a</TD><TD style="TEXT-ALIGN: right">11:00 AM</TD></TR><TR style="HEIGHT: 18px"><TD style="FONT-SIZE: 8pt; BACKGROUND-COLOR: #cacaca; TEXT-ALIGN: center">3</TD><TD>1200p</TD><TD style="TEXT-ALIGN: right">12:00 PM</TD></TR><TR style="HEIGHT: 18px"><TD style="FONT-SIZE: 8pt; BACKGROUND-COLOR: #cacaca; TEXT-ALIGN: center">4</TD><TD>100p</TD><TD style="TEXT-ALIGN: right">1:00 PM</TD></TR><TR style="HEIGHT: 18px"><TD style="FONT-SIZE: 8pt; BACKGROUND-COLOR: #cacaca; TEXT-ALIGN: center">5</TD><TD>200p</TD><TD style="TEXT-ALIGN: right">2:00 PM</TD></TR></TBODY></TABLE>
<TABLE style="FONT-SIZE: 10pt; BORDER-LEFT-COLOR: #00ff00; BORDER-BOTTOM-COLOR: #00ff00; COLOR: #000000; BORDER-TOP-STYLE: groove; BORDER-TOP-COLOR: #00ff00; FONT-FAMILY: Arial; BORDER-RIGHT-STYLE: groove; BORDER-LEFT-STYLE: groove; BACKGROUND-COLOR: #fffcf9; BORDER-RIGHT-COLOR: #00ff00; BORDER-BOTTOM-STYLE: groove"><TBODY><TR><TD>Spreadsheet Formulas</TD></TR><TR><TD><TABLE style="FONT-SIZE: 9pt; FONT-FAMILY: Arial" cellSpacing=0 cellPadding=2 border=1><TBODY><TR style="FONT-SIZE: 10pt; BACKGROUND-COLOR: #cacaca"><TD>Cell</TD><TD>Formula</TD></TR><TR><TD>B1</TD><TD>=IF(OR(RIGHT(A1,1)="a",RIGHT(A1,1)="p"),TRIM(IF(ISERROR(Hourformat(A1)+MinFormat(A1)),"",Hourformat(A1)&":"&IF(MinFormat(A1)<10,"0"&MinFormat(A1),MinFormat(A1))&IF(RIGHT(A1)="a"," AM"," PM"))),"")</TD></TR><TR><TD>B2</TD><TD>=IF(OR(RIGHT(A2)="a",RIGHT(A2)="p"),TRIM(IF(ISERROR(Hourformat(A2)+MinFormat(A2)),"",Hourformat(A2)&":"&IF(MinFormat(A2)<10,"0"&MinFormat(A2),MinFormat(A2))&IF(RIGHT(A2)="a"," AM"," PM"))),"")</TD></TR><TR><TD>B3</TD><TD>=IF(OR(RIGHT(A3)="a",RIGHT(A3)="p"),TRIM(IF(ISERROR(Hourformat(A3)+MinFormat(A3)),"",Hourformat(A3)&":"&IF(MinFormat(A3)<10,"0"&MinFormat(A3),MinFormat(A3))&IF(RIGHT(A3)="a"," AM"," PM"))),"")</TD></TR><TR><TD>B4</TD><TD>=IF(OR(RIGHT(A4)="a",RIGHT(A4)="p"),TRIM(IF(ISERROR(Hourformat(A4)+MinFormat(A4)),"",Hourformat(A4)&":"&IF(MinFormat(A4)<10,"0"&MinFormat(A4),MinFormat(A4))&IF(RIGHT(A4)="a"," AM"," PM"))),"")</TD></TR><TR><TD>B5</TD><TD>=IF(OR(RIGHT(A5)="a",RIGHT(A5)="p"),TRIM(IF(ISERROR(Hourformat(A5)+MinFormat(A5)),"",Hourformat(A5)&":"&IF(MinFormat(A5)<10,"0"&MinFormat(A5),MinFormat(A5))&IF(RIGHT(A5)="a"," AM"," PM"))),"")</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>

Excel tables to the web >> http://www.excel-jeanie-html.de/index.php?f=1" target="_blank"> Excel Jeanie HTML 4
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,223,834
Messages
6,174,913
Members
452,590
Latest member
CraiginColorado

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top