Converting text to numbers

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,294
Office Version
  1. 365
Platform
  1. Windows
I would like to find code that will generate the data in col C using the text data in A & B.

C/RABC
3BegEndHours
4Fri 7:00 pmFri 7:00 pm0.00
5Fri 7:00 pmFri 9:00 pm2.00
6Fri 7:00 pmSat 7:00 am12.00
7Mon 3:15 pmMon 7:45 pm4.50
8Fri 7:00 pmSat 7:00 pm24.00
9Fri 7:00 pmSun 7:00 pm48.00
10Fri 7:00 pmMon 7:00 pm72.00
11Fri 7:00 pmTue 7:00 pm96.00
12Fri 7:00 pmWed 7:00 pm120.00
13Fri 7:00 pmThu 7:00 pm144.00
14Fri 7:00 pmFri 7:00 pm168.00

<tbody>
</tbody>

I think it will have to be a UDF. What's the best way to convert the days (Sun, Mon, ...) into numbers (1, 2, ...)?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I would like to find code that will generate the data in col C using the text data in A & B.

C/RABC
3BegEndHours
4Fri 7:00 pmFri 7:00 pm0.00
5Fri 7:00 pmFri 9:00 pm2.00
6Fri 7:00 pmSat 7:00 am12.00
7Mon 3:15 pmMon 7:45 pm4.50
8Fri 7:00 pmSat 7:00 pm24.00
9Fri 7:00 pmSun 7:00 pm48.00
10Fri 7:00 pmMon 7:00 pm72.00
11Fri 7:00 pmTue 7:00 pm96.00
12Fri 7:00 pmWed 7:00 pm120.00
13Fri 7:00 pmThu 7:00 pm144.00
14Fri 7:00 pmFri 7:00 pm168.00

<tbody>
</tbody>

I think it will have to be a UDF. What's the best way to convert the days (Sun, Mon, ...) into numbers (1, 2, ...)?
Are these times tied to real calendar dates in anyway? If so, can you tell us how they are linked to such ral dates? I ask because you times values can span multiple days (as C/R 10 shows) which means that we would know if any of the times crossed from February into March and whether it was a Leap Year or not.
 
Last edited:
Upvote 0

tygrrboi

Well-known Member
Joined
Sep 8, 2015
Messages
1,196
I only tested this with your actual example data... so I am not sure if it works all the time... but it sure was fun!

Give this a try:


<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Lucida Grande'}span.s1 {color: #006107}span.s2 {color: #ab30d6}span.s3 {color: #a54a29}span.s4 {color: #33af4a}span.s5 {color: #0057d6}</style>=24*(MOD(7+(MATCH(LEFT(B2,3), {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}, 0)-MATCH(LEFT(A2,3), {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}, 0)),7)-(RIGHT(B2,2)="am"))+ABS((60*((RIGHT(B2,2)="pm")*12+MID(B2,FIND(" ",B2)+1,FIND(":",B2)-FIND(" ",B2)-1))+MID(B2,FIND(":",B2)+1,2))-(60*((RIGHT(A2,2)="pm")*12+MID(A2,FIND(" ",A2)+1,FIND(":",A2)-FIND(" ",A2)-1))+MID(A2,FIND(":",A2)+1,2)))/60
 
Upvote 0

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,294
Office Version
  1. 365
Platform
  1. Windows
C4=B4-A4
Format cell column C is Custom [hh]:mm

The data in A & B is text, literal text. The data in A4 is "Fri 7:00 pm". If I apply your formula, I get "VALUE!".
 
Upvote 0

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,294
Office Version
  1. 365
Platform
  1. Windows
Are these times tied to real calendar dates in anyway? If so, can you tell us how they are linked to such ral dates? I ask because you times values can span multiple days (as C/R 10 shows) which means that we would know if any of the times crossed from February into March and whether it was a Leap Year or not.

The times are not tied to any particular date. I should have been more explicit about that. They are all within a week of each other. I tried to illustrate that with the data rows 4 & 8-14. The End time (Col B) is the next occurrence of that time up to a week later.

I also see that there is an error in my table above. Rows 4 and 14 give different results for the same data. (sigh) Sorry about that.

Here's a better table.

C/RABCDE
3BegEndHoursDaysComments
4Fri 7:00 pmFri 9:00 pm2.000.0833From 7 pm to 9 pm on same day
5Mon 3:15 pmMon 7:45 pm4.500.1875From 3:15 pm on Mon to 7:45 pm on the same day
6Fri 7:00 pmSat 7:00 pm24.001.0000From 7 pm on Fri to 7 pm on Sat, the next day
7Fri 7:00 pmSun 7:00 pm48.002.0000From 7 pm on Fri to 7 pm on Sun, 2 days later
8Fri 7:00 pmMon 7:00 pm72.003.0000From 7 pm on Fri to 7 pm on Mon, 3 days later
9Fri 7:00 pmTue 7:00 pm96.004.0000From 7 pm on Fri to 7 pm on Tue, 4 days later
10Fri 7:00 pmWed 7:00 pm120.005.0000From 7 pm on Fri to 7 pm on Wed, 5 days later
11Fri 7:00 pmThu 7:00 pm144.006.0000From 7 pm on Fri to 7 pm on Thu, 6 days later
12Fri 7:00 pmFri 7:00 pm168.007.0000From 7 pm on Fri to 7 pm on Fri, 7 days later
13Fri 7:00 pmFri 6:00 pm167.006.9583From 7 pm on Fri to 6 pm on Fri, 7 days later

<tbody>
</tbody>
 
Upvote 0

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,294
Office Version
  1. 365
Platform
  1. Windows
I only tested this with your actual example data... so I am not sure if it works all the time... but it sure was fun!

Give this a try:


<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Lucida Grande'}span.s1 {color: #006107}span.s2 {color: #ab30d6}span.s3 {color: #a54a29}span.s4 {color: #33af4a}span.s5 {color: #0057d6}</style>=24*(MOD(7+(MATCH(LEFT(B2,3), {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}, 0)-MATCH(LEFT(A2,3), {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}, 0)),7)-(RIGHT(B2,2)="am"))+ABS((60*((RIGHT(B2,2)="pm")*12+MID(B2,FIND(" ",B2)+1,FIND(":",B2)-FIND(" ",B2)-1))+MID(B2,FIND(":",B2)+1,2))-(60*((RIGHT(A2,2)="pm")*12+MID(A2,FIND(" ",A2)+1,FIND(":",A2)-FIND(" ",A2)-1))+MID(A2,FIND(":",A2)+1,2)))/60


Wow! I am impressed, but I worry about your definition of "fun".:LOL:

I couldn't get it to work. If I just paste it into a cell, I get "#N/A". If I paste it into the formula bar and use Ctrl+Shift+Enter, I just get the literal formula.

What are the {}"? Is this an array formula?
 
Upvote 0

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
The times are not tied to any particular date. I should have been more explicit about that. They are all within a week of each other. I tried to illustrate that with the data rows 4 & 8-14. The End time (Col B) is the next occurrence of that time up to a week later.
I still think you could be off by 24 hours if the two times cross from February to March in a Leap Year, but since that does not appear concern you, give this formula for hours a try...

=24*(MOD((FIND(LEFT(B4,2),"SuMoTuWeThFrSa")-FIND(LEFT(A4,2),"SuMoTuWeThFrSa"))/2,7)+7*(AND(LEFT(A4,3)=LEFT(B4,3),MID(A4,LEN(A4)-6,8)>=MID(B4,LEN(B4)-6,8)))+TIMEVALUE((RIGHT(B4,8)))-TIMEVALUE(RIGHT(A4,8)))
 
Last edited:
Upvote 0

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
12,627
Something like this maybe?

ABCD
1C/RAB
23BegEndHours
34Fri 7:00 pmFri 7:00 pm0.00
45Fri 7:00 pmFri 9:00 pm2.00
56Fri 7:00 pmSat 7:00 am12.00
67Mon 3:15 pmMon 7:45 pm4.50
78Fri 7:00 pmSat 7:00 pm24.00
89Fri 7:00 pmSun 7:00 pm48.00
910Fri 7:00 pmMon 7:00 pm72.00
1011Fri 7:00 pmTue 7:00 pm96.00
1112Fri 7:00 pmWed 7:00 pm120.00
1213Fri 7:00 pmThu 7:00 pm144.00
1314Fri 7:00 pmFri 6:00 pm167.00

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet3

Worksheet Formulas
CellFormula
D3=MOD((MOD(MATCH(LEFT(C3,3),{"Sun","Mon","Tue","Wed","Thu","Fri","Sat"},0)-MATCH(LEFT(B3,3),{"Sun","Mon","Tue","Wed","Thu","Fri","Sat"},0),7)+MID(LEFT(C3,LEN(C3)-3),5,99)+IF(RIGHT(C3,2)="pm",0.5,0)-(MID(LEFT(B3,LEN(B3)-3),5,99)+IF(RIGHT(B3,2)="pm",0.5,0)))*24,168)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,294
Office Version
  1. 365
Platform
  1. Windows
C4=B4-A4
Format cell column C is Custom [hh]:mm

I was curious about your "[hh]:mm" format string. I could not find any description of this anywhere. It appears that it stops Excel from truncating the hours at "23". Is that correct? Here's my data formatted a couple of ways.

C/RCDEFGH
3BegEndHoursDaysh:mm[h]:mm
4Fri 01/01/16 07:00 PMFri 01/01/16 09:00 PM2.000.08332:002:00
5Mon 01/04/16 03:15 PMMon 01/04/16 07:45 PM4.500.18754:304:30
6Fri 01/01/16 07:00 PMSat 01/02/16 07:00 PM24.001.00000:0024:00
7Fri 01/01/16 07:00 PMSun 01/03/16 07:00 PM48.002.00000:0048:00
8Fri 01/01/16 07:00 PMMon 01/04/16 07:00 PM72.003.00000:0072:00
9Fri 01/01/16 07:00 PMTue 01/05/16 07:00 PM96.004.00000:0096:00
10Fri 01/01/16 07:00 PMWed 01/06/16 07:00 PM120.005.00000:00120:00
11Fri 01/01/16 07:00 PMThu 01/07/16 07:00 PM144.006.00000:00144:00
12Fri 01/01/16 07:00 PMFri 01/08/16 07:00 PM168.007.00000:00168:00
13Fri 01/01/16 07:00 PMFri 01/08/16 06:00 PM167.006.958323:00167:00

<tbody>
</tbody>

Is this documented anywhere? Or is this one of the many typical M$FT stealth features?
 
Upvote 0

Forum statistics

Threads
1,190,857
Messages
5,983,252
Members
439,833
Latest member
CDaviess

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
Top