I have to convert some formatted text to excel time, but I could not find a solution by myself.
Here are some examples of the input text and how it should be converted:
- 1 Hour 14 minutes ==> 01:14:00
- 1 minute. ==> 00:01:00
- 1 Hour 1 minute ==> 01:01:00
- 2 minutes ==> 00:02:00
- 3 minutes 12 seconds ==> 00:03:12
- 29 seconds ==> 00:00:29
Observe that some times there are both minutes and seconds and some others only one of minutes/seconds, besides some times you find minutes (plural) and some others just minute (singular). Finally, some punctuation signs could be in the text some times.
The data is in a spreadsheet column and I want to extract the excel formatted time in a different column on the spreadsheet.
I've tried different versions of TimeValue() and DateValue() and some nested replace() all of them in a cell formula, but none of them worked for all cases.
Could you give me an idea or some advice on how to approach this problem?
Thanks,
Paul
I played with your question for a day and see that others have done the same. I admire the solution offered by Ron Rosenfeld using FilterXML which I briefly considered and discarded as too outlandish. It isn't. But neitehr is it neat. For "neat" look no further than the UDF below. Call it from the worksheet with something like =TextToTime(A1) and be sure to format the cell you put this in as Time, perhaps like hh:mm:ss.
Function TextToTime(Cell As Range) As Double
Dim Fun(2) As String
Dim Txt() As String
Dim Tmp As Variant
Dim i As Integer
Txt = Split(Cell.Value)
For i = 1 To UBound(Txt)
If Not IsNumeric(Txt(i)) Then
Tmp = 0
Tmp = InStr("HMS", UCase(Left(Trim(Txt(i)), 1)))
If Tmp Then
Fun(Tmp - 1) = Val(Txt(i - 1))
End If
End If
Next i
For i = LBound(Fun) To UBound(Fun)
If Fun(i) = "" Then Fun(i) = 0
Next i
TextToTime = TimeValue(Join(Fun, ":"))
End Function
This function is very versatile. It can translate strings like "5 hours 10 minutes 15 seconds" or "5 hours, 10 minutes, 15 seconds" (with commas) or even "5 hours & 10 minutes and 15 seconds". It needs the spaces around the numbers but doesn't balk at extra spaces or typos. It fails on "75 minutes and 66 seconds" but that could be dealt with if it's an issue. The key advantage of VBA is that almost anything can be dealt with using just a few extra lines of code. Worksheeet functions don't have that capability.
Since you did not include VBA as a tag, here are some formula solutions.
If you have Windows Excel 2013+ with the FILTERXML function, you can use the following:
=IFERROR(FILTERXML("<t><s>" & SUBSTITUTE(LOWER(A1)," ","</s><s>") & "</s></t>","//s[contains(.,'hour')]/preceding-sibling::*[1]")/24,0)+
IFERROR(FILTERXML("<t><s>" & SUBSTITUTE(LOWER(A1)," ","</s><s>") & "</s></t>","//s[contains(.,'min')]/preceding-sibling::*[1]")/1440,0)+
IFERROR(FILTERXML("<t><s>" & SUBSTITUTE(LOWER(A1)," ","</s><s>") & "</s></t>","//s[contains(.,'sec')]/preceding-sibling::*[1]")/86400,0)
If you do not have the FILTERXML function:
Create a named formula:
Formulas → Defined Names → Define Name → New Name → seq_99 → Refers to: → =IF(ROW(INDEX($A:$A,1,1):INDEX($A:$A,255,1))=1,1,(ROW(INDEX($A:$A,1,1):INDEX($A:$A,255,1))-1)*99)
Then use this formula:
=IFERROR(INDEX(TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),-1+MATCH("hour*",TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),0))/24,0)+
IFERROR(INDEX(TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),-1+MATCH("min*",TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),0))/1440,0)+
IFERROR(INDEX(TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),-1+MATCH("sec*",TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),0))/86400,0)
Algorhythm:
Find the string hour or min or sec
Return the node prior to each of those strings
divide by the appropriate factor to create an excel time value
Note: Custom format column B as hh:mm:yy
As pointed out by #ScottCraner in the comments, the formulas can be shortened by creating an array formula:
Using FILTERXML:
=SUM(IFERROR(FILTERXML("<t><s>" & SUBSTITUTE(LOWER(A1)," ","</s><s>") & "</s></t>","//s[contains(.,"&{"'hour'","'min'","'sec'"}&")]/preceding-sibling::*[1]")/{24,1440,8640},0))
without FILTERXML:
=SUM(IFERROR(INDEX(TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),-1+MATCH({"hour*","min*","sec*"},TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),0))/{24,1440,8640},0))
In some earlier versions of Excel, you may need to "confirm" this array-formula it by holding down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula as observed in the formula bar
Well, you can take this and edit it to suit your needs:
Here is the formula :
=IF(IFERROR(FIND("da",A2,1),0)>0,LEFT(A2,FIND(" ",A2,1)-1)*24,0)+IFERROR(1*MID(A2,FIND(" ",A2,FIND(" ",A2,1)+1),FIND(" ",A2,FIND(" ",A2,FIND(" ",A2,1)+1)+1)-FIND(" ",A2,FIND(" ",A2,1)+1)),0*1)
I just wanted the date converted to hours so you can finish to what you want.
If you decide to use VBA and you keep the same format in Column A you could use the below code:
Code:
Sub Converter()
Dim i As Long, y As Long, LastRow As Long
Dim arrData As Variant, arrLine As Variant
Dim Hours As String, Minutes As String, Seconds As String
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
arrData = .Range("A1:A" & LastRow)
For i = LBound(arrData) To UBound(arrData)
Hours = "00"
Minutes = "00"
Seconds = "00"
arrLine = Split(arrData(i, 1), " ")
For y = LBound(arrLine) To UBound(arrLine)
If InStr(1, UCase(arrLine(y)), "HOUR") > 0 Then
Hours = Right("0" & arrLine(y - 1), 2)
ElseIf InStr(1, UCase(arrLine(y)), "MINUTE") > 0 Then
Minutes = Right("0" & arrLine(y - 1), 2)
ElseIf InStr(1, UCase(arrLine(y)), "SECOND") > 0 Then
Seconds = Right("0" & arrLine(y - 1), 2)
End If
Next y
.Range("B" & i).Value = Hours & ":" & Minutes & ":" & Seconds
Next i
End With
End Sub
Output:
Related
I use Excel 2013.
When I subtract two dates in excel "1.11.2019" and "1.10.2019" I get the result 31 as you should.
But when I do the same thing in VBA I get 10000. So it sees them as regular numbers "1112019" and "1102019". How can I get the same result in VBA too?
Use DateDiff() function with d interval to get days difference between two date. Try like below.
Sub DateSubstract()
MsgBox DateDiff("d", Range("B1"), Range("A1"))
End Sub
You may have to format the string into a Date format
Dim diff As Long
Dim strDate1, strDate2 As String
Dim fDate1, FDate2 As Date
Set sh = ThisWorkbook.Sheets("myDate")
' convert in string with 8 caracters
strDate1 = Right("0" & sh.Range("A1").Value, 8)
strDate2 = Right("0" & sh.Range("B1").Value, 8)
'format Year, month, day
fDate1 = DateSerial(Right(strDate1, 4), Mid(strDate1, 3, 2), Left(strDate1, 2))
fDate2 = DateSerial(Right(strDate2, 4), Mid(strDate2, 3, 2), Left(strDate2, 2))
diff = DateDiff("d", fDate2, fDate1)
Debug.Print ("difference :" & diff)
I have to convert some formatted text to excel time, but I could not find a solution by myself.
Here are some examples of the input text and how it should be converted:
- 1 Hour 14 minutes ==> 01:14:00
- 1 minute. ==> 00:01:00
- 1 Hour 1 minute ==> 01:01:00
- 2 minutes ==> 00:02:00
- 3 minutes 12 seconds ==> 00:03:12
- 29 seconds ==> 00:00:29
Observe that some times there are both minutes and seconds and some others only one of minutes/seconds, besides some times you find minutes (plural) and some others just minute (singular). Finally, some punctuation signs could be in the text some times.
The data is in a spreadsheet column and I want to extract the excel formatted time in a different column on the spreadsheet.
I've tried different versions of TimeValue() and DateValue() and some nested replace() all of them in a cell formula, but none of them worked for all cases.
Could you give me an idea or some advice on how to approach this problem?
Thanks,
Paul
I played with your question for a day and see that others have done the same. I admire the solution offered by Ron Rosenfeld using FilterXML which I briefly considered and discarded as too outlandish. It isn't. But neitehr is it neat. For "neat" look no further than the UDF below. Call it from the worksheet with something like =TextToTime(A1) and be sure to format the cell you put this in as Time, perhaps like hh:mm:ss.
Function TextToTime(Cell As Range) As Double
Dim Fun(2) As String
Dim Txt() As String
Dim Tmp As Variant
Dim i As Integer
Txt = Split(Cell.Value)
For i = 1 To UBound(Txt)
If Not IsNumeric(Txt(i)) Then
Tmp = 0
Tmp = InStr("HMS", UCase(Left(Trim(Txt(i)), 1)))
If Tmp Then
Fun(Tmp - 1) = Val(Txt(i - 1))
End If
End If
Next i
For i = LBound(Fun) To UBound(Fun)
If Fun(i) = "" Then Fun(i) = 0
Next i
TextToTime = TimeValue(Join(Fun, ":"))
End Function
This function is very versatile. It can translate strings like "5 hours 10 minutes 15 seconds" or "5 hours, 10 minutes, 15 seconds" (with commas) or even "5 hours & 10 minutes and 15 seconds". It needs the spaces around the numbers but doesn't balk at extra spaces or typos. It fails on "75 minutes and 66 seconds" but that could be dealt with if it's an issue. The key advantage of VBA is that almost anything can be dealt with using just a few extra lines of code. Worksheeet functions don't have that capability.
Since you did not include VBA as a tag, here are some formula solutions.
If you have Windows Excel 2013+ with the FILTERXML function, you can use the following:
=IFERROR(FILTERXML("<t><s>" & SUBSTITUTE(LOWER(A1)," ","</s><s>") & "</s></t>","//s[contains(.,'hour')]/preceding-sibling::*[1]")/24,0)+
IFERROR(FILTERXML("<t><s>" & SUBSTITUTE(LOWER(A1)," ","</s><s>") & "</s></t>","//s[contains(.,'min')]/preceding-sibling::*[1]")/1440,0)+
IFERROR(FILTERXML("<t><s>" & SUBSTITUTE(LOWER(A1)," ","</s><s>") & "</s></t>","//s[contains(.,'sec')]/preceding-sibling::*[1]")/86400,0)
If you do not have the FILTERXML function:
Create a named formula:
Formulas → Defined Names → Define Name → New Name → seq_99 → Refers to: → =IF(ROW(INDEX($A:$A,1,1):INDEX($A:$A,255,1))=1,1,(ROW(INDEX($A:$A,1,1):INDEX($A:$A,255,1))-1)*99)
Then use this formula:
=IFERROR(INDEX(TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),-1+MATCH("hour*",TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),0))/24,0)+
IFERROR(INDEX(TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),-1+MATCH("min*",TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),0))/1440,0)+
IFERROR(INDEX(TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),-1+MATCH("sec*",TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),0))/86400,0)
Algorhythm:
Find the string hour or min or sec
Return the node prior to each of those strings
divide by the appropriate factor to create an excel time value
Note: Custom format column B as hh:mm:yy
As pointed out by #ScottCraner in the comments, the formulas can be shortened by creating an array formula:
Using FILTERXML:
=SUM(IFERROR(FILTERXML("<t><s>" & SUBSTITUTE(LOWER(A1)," ","</s><s>") & "</s></t>","//s[contains(.,"&{"'hour'","'min'","'sec'"}&")]/preceding-sibling::*[1]")/{24,1440,8640},0))
without FILTERXML:
=SUM(IFERROR(INDEX(TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),-1+MATCH({"hour*","min*","sec*"},TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),0))/{24,1440,8640},0))
In some earlier versions of Excel, you may need to "confirm" this array-formula it by holding down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula as observed in the formula bar
Well, you can take this and edit it to suit your needs:
Here is the formula :
=IF(IFERROR(FIND("da",A2,1),0)>0,LEFT(A2,FIND(" ",A2,1)-1)*24,0)+IFERROR(1*MID(A2,FIND(" ",A2,FIND(" ",A2,1)+1),FIND(" ",A2,FIND(" ",A2,FIND(" ",A2,1)+1)+1)-FIND(" ",A2,FIND(" ",A2,1)+1)),0*1)
I just wanted the date converted to hours so you can finish to what you want.
If you decide to use VBA and you keep the same format in Column A you could use the below code:
Code:
Sub Converter()
Dim i As Long, y As Long, LastRow As Long
Dim arrData As Variant, arrLine As Variant
Dim Hours As String, Minutes As String, Seconds As String
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
arrData = .Range("A1:A" & LastRow)
For i = LBound(arrData) To UBound(arrData)
Hours = "00"
Minutes = "00"
Seconds = "00"
arrLine = Split(arrData(i, 1), " ")
For y = LBound(arrLine) To UBound(arrLine)
If InStr(1, UCase(arrLine(y)), "HOUR") > 0 Then
Hours = Right("0" & arrLine(y - 1), 2)
ElseIf InStr(1, UCase(arrLine(y)), "MINUTE") > 0 Then
Minutes = Right("0" & arrLine(y - 1), 2)
ElseIf InStr(1, UCase(arrLine(y)), "SECOND") > 0 Then
Seconds = Right("0" & arrLine(y - 1), 2)
End If
Next y
.Range("B" & i).Value = Hours & ":" & Minutes & ":" & Seconds
Next i
End With
End Sub
Output:
I have a table with several operation codes and its hours, and I need to sum every Thursday the hours spent on each code.
Despite being able to figure it out an IF formula would do the job I got stuck with the sum of the ranges, I could get it working via VBA but I can´t apply that same solution on Formula:
WorksheetFunction.Sum(Range("E" & cCell.Row & ":E" & cCell.Row - 6))
Dim Counter As Integer
Dim cCell As Range
Dim intToday As Integer
Dim CountDate As Integer
Dim strWsName As String
strWsName = ActiveSheet.Name
Dim xWs As Worksheet
Set xWs = Worksheets(strWsName)
'Clause 101
For Counter = 4 To 34
Set cCell = xWs.Cells(Counter, 4)
If WorksheetFunction.WeekDay(cCell.Value) = 5 Then
If cCell.Row = 4 Then
xWs.Range("Q" & cCell.Row) = WorksheetFunction.Sum(Range("E" & cCell.Row & ":E" & cCell.Row))
Else
If cCell.Row >= 34 Then
xWs.Range("Q" & cCell.Row) = WorksheetFunction.Sum(Range("E" & cCell.Row & ":E" & cCell.Row))
Else
If cCell.Row - 6 <= 0 Then
xWs.Range("Q" & cCell.Row) = WorksheetFunction.Sum(Range("E" & cCell.Row & ":E4"))
Else
xWs.Range("Q" & cCell.Row) = WorksheetFunction.Sum(Range("E" & cCell.Row & ":E" & cCell.Row - 6))
End If
End If
End If
End If
Next Counter
End Sub
I would like to know how I could transform that piece of code to a formula on Excel.
After doing a research on formulas and variables ranges I have managed to set this formula and it is working like a charm:
=IF(WEEKDAY($D9)=5;IF(ROW(E9)<=6;SUM(OFFSET(E9;;;-ROW()));SUM(OFFSET(E9;;;-7)));"")
NOTE: My locale settings uses ";" instead of "," on formulas, so bear in mind you must change it to your locale settings.
Explanation:
The "=IF(WEEKDAY($D9)=5" formula will assess if the date is a Thursday, if it is not it will exhbit "" on the column;
The IF(ROW(E9)<=6 clause will serve to identify the limits to the top of my spreadsheet, depding on its value it will SUM diferent ranges:
If the date´s row is smaller than 6 it will run this:
SUM(OFFSET(E9;;;-ROW())). The offset will be the same as the row where the
date is.
Now if the row is bigger than 6 then I will be able to set a fixed
offset of 7 rows: SUM(OFFSET(E9;;;-7))
If one needs to change the calculation to Wednesday all you have to do is to change the number "5" on "=IF(WEEKDAY($D9)=5" numbers of the weekdays intended, here is the list:
1 to Sundays
2 to Mondays
3 to Tuesdays
4 to Wednesdays
5 to Thursdays
6 to Fridays
7 to Saturdays
Thanks to articles found on Excel Jet and Extend Office I managed to build this solution!
I hope more people can use this solution!
The best way to solve this was by creating a UDF, I had the help from WideBoyDixon at ExcelForum:
Public Function SumWeek(sumRange As Range, dateRange As Range, endDate)
Application.Volatile
Dim prevSheetName As String
Dim prevSheet As Worksheet
SumWeek = Application.WorksheetFunction.SumIfs(sumRange, dateRange, "<=" & CStr(CLng(endDate)), dateRange, ">" & CStr(CLng(endDate - 7)))
prevSheetName = Mid("DecJanFebMarAprMayJunJulAugSepOctNov", Month(endDate) * 3 - 2, 3) & CStr(Year(endDate) - IIf(Month(endDate) = 1, 1, 0))
On Error Resume Next
Set prevSheet = Worksheets(prevSheetName)
If Not (prevSheet Is Nothing) Then
SumWeek = SumWeek + Application.WorksheetFunction.SumIfs(prevSheet.Range(sumRange.Address), prevSheet.Range(dateRange.Address), "<=" & CStr(CLng(endDate)), prevSheet.Range(dateRange.Address), ">" & CStr(CLng(endDate - 7)))
End If
End Function
I am trying to make VBA write a formula into different cells that will find the maximum value for a Range decided by some variables. My variables I and J are (numbers/Integers).
Here is my code.
Sub AddMAX()
Dim I As Integer
Dim J As Integer
Dim L As Integer
I = InputBox("Number of columns to check max value")
J = InputBox("Number of Rows to add formula inn and find max value of that row")
For L = 5 To 4 + J
Worksheets(1).Cells(L, 4 + I).Formula = "=" & Max(Range(Cells(L, 4), Cells(L, 3 + I)))
Next L
End Sub
Have tried to re-write the second part (part behind the equal sign) several times. Usually I get the message Compile error: Sub or Function not defined and it marks the "Max". I thought Max (also tried with big letters) was an in-built function like SUM and so on.
I'm trying to make it write an Excel formula like this into the cells:
For I=2 and J=3:
Cell F5: =MAX(D5:E5)
Cell F6: =MAX(D6:E6)
Cell F7: =MAX(D7:E7)
i.e. I want a formula in the cells like I had wrote it in the cells manually to calculate max value, so that if the value in Cells D5, to D7 and E5 to E7 change, the new max value will be found without any scripts having to run.
Let me know if something is unclear.
You should not be putting Range and Cells in a formula string, they mean nothing to the Excel formula engine. You need the Address of the cells:
Dim I As Long
Dim J As Long
Dim L As Long
I = InputBox("Number of columns to check max value")
J = InputBox("Number of Rows to add formula inn and find max value of that row")
L = 5
With Worksheets(1)
.Range(.Cells(L, 4 + I), .Cells(4 + J, 4 + I)).Formula = "=MAX(" & .Cells(L, 4).Address(False, False) & ":" & .Cells(L, I + 3).Address(False, False) & ")"
End With
The formula is actually the same for all cells, which is why it is possible to assign it in one assignment for the entire range. It looks different in the A1 reference notation, but if you switch to R1C1 in the Excel settings, you will see they are the same. Which also means it is easier to create that formula using the R1C1 notation in the first place:
Dim I As Long
Dim J As Long
Dim L As Long
I = InputBox("Number of columns to check max value")
J = InputBox("Number of Rows to add formula inn and find max value of that row")
L = 5
With Worksheets(1)
.Range(.Cells(L, 4 + I), .Cells(4 + J, 4 + I)).FormulaR1C1 = "=MAX(RC[-" & I & "]:RC[-1])"
End With
But it would appear to me that you should instead use the Excel interface the intended way. Select the cells in which the MAX formula should be. Keeping the entire range selected, put the MAX formula into any of its cells as if you were creating it for just that cell, but instead of pressing Enter, press Ctrl+Enter.
You have to be careful to distinct between the part that is seen by VBA and the final formula.
If you write
Worksheets(1).Cells(L, 4 + I).Formula = "=" & Max(Range(Cells(L, 4), Cells(L, 3 + I)))
Max (and all the following stuff) is seen by the VBA-interpreter, not Excel. But there is no Max-function, and you get an (compiler)-error.
If you write
Worksheets(1).Cells(L, 4 + I).Formula = "=Max(Range(Cells(L, 4), Cells(L, 3 + I)))"
the VBA-interpreter sees the whole stuff as a string. It cannot take care about variables like L or I because is doesn't see them. So you end up with a formula that is exactly like you write it - and Excel (not VBA) will show you an error because it doesn't understand L or I.
What you need is a statement (in VBA) that creates a string that contains the actual values of your variables, and assign it to the cell.formula. I strongly advice that you first assign this to a string variable - it makes debugging much easier:
Dim formula As String
formula = "=Max(Range(Cells(" & L & ", 4), Cells(" & L & ", 3 + " & I & ")))"
Debug.Print formula
Worksheets(1).Cells(L, 4 + I).Formula = formula
Update: Sorry, I haven't looked to the content of the formula at all, of course the Range and Cells-objects are VBA objects. What you need in your formula is the address of the range, so change the line to
formula = "=MAX(" & Range(Cells(L, 4), Cells(L, 3 + i)).Address & ")"
Now VBA will create a Range and put the address into the formula string.
I have two columns that contain information in the following format.
Column A - 35 days
Column B - 29 days
How can i subtract the two columns to show just a number, like 35 days - 29 days = 6.
The following equation should do the trick
= VALUE(MID(A1, 1, FIND(" ",A1))) - VALUE(MID(B1, 1,FIND(" ",B1)))
First we find the index of the space " " in the cell using FIND(" ", A1). Then we cut the string in the cell and we take the first index to the index of the space by MID(A1, 1, ...). Then we convert this value to a number using VALUE(...).
If all your days always have 2 digits, none of them are >= 100, then we can simplify this by
= VALUE(MID(A1, 1, 2)) - VALUE(MID(B1, 1,2))
But, I think the general case is preferable.
You can use the Replace function and replace days with an empty string, then you can convert your string to a integer with CInt:
?CInt(Replace("39 days", "days", ""))-CInt(Replace("25 days", "days", ""))
14
Edit:
or like Jeeped suggested in the commment you could use:
?Val("39 days")-Val("25 days")
14
You can also use SUBSTITUTE() function.
=(TRIM(SUBSTITUTE(A1,"days",""))*1)-TRIM(SUBSTITUTE(B1,"days",""))*1
Edit:
Only SUBSTITUTE() will also work.
=SUBSTITUTE(A1,"days","")-SUBSTITUTE(B1,"days","")
Change the cell number format to Custom with a format mask of 0 \d\a\y\s. Now type in 35 into A1 and 29 into B1. Now, while the cells display 35 days and 29 days they can be conventionally used like,
'on the worksheet
=a1-b1
'in vba
i = range("a1").value - range("b1").value
'in vba to see *35 days* in the Immediate window
?range("a1").text
You could even get a little fancier with a custom number format of,
[>1]0 \d\a\y\s;[=1]0 \d\a\y;0 \d\a\y\s;
Try this code:
Sub SumDays()
Dim lastRow As Long, i As Long, cellA As String, cellB As String
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastRow
cellA = Cells(i, 1).Value
cellB = Cells(i, 2).Value
Cells(i, 3).Value = CInt(Mid(cellA, 1, InStr(1, cellA, " ") - 1)) + CInt(Mid(cellB, 1, InStr(1, cellB, " ") - 1))
Next
End Sub