Adding hh:mm:ss in VBA

tomhoneyfield

New Member
Joined
Jul 19, 2012
Messages
3
Hi,

I'm currently writing an automated process that opens up a productivity report (xls) and has to add together a bunch of different times to work out a total.

Now, when I try to add together the values from individual cells, rather than adding together the raw value within the cell, I get a concatenated result like the following;

The cell in column "O" = "00:05:50"
The cell in column "Q" = "00:03:04"

myVar = wbInb.Sheets("Inbound SLA").Range("O" & raRow).Value + _
wbInb.Sheets("Inbound SLA").Range("Q" & raRow).Value

MsgBox myVar

Result = "00:05:5000:03:04"

It could be important to note that the information sits in cells that are formatted general. Also O and Q are merged with the cell immediately to their right. I don't know if this has any impact.

Interestingly, without changing any formatting of any cells, if I set the formula of a random general formatted cell to;

"=O25+Q25" (as an example)

Result = 0.006180556 - which then when formatted to hh:mm:ss translates to "00:08:54" - Hey presto! That's what we want.

Now it's a bit ridiculous but to get around this in my macro, I coded up like the following;

wbInb.Sheets("Inbound SLA").Range("Z" & raRow).Formula = "=O" & raRow & "+Q" & raRow
myVar = wbInb.Sheets("Inbound SLA").Range("Z" & raRow).Value

MsgBox Format(myVar, "HH:MM:SS")

Result = "00:08:54" - Bob's your uncle (he's not)

I suppose my question is, does anyone know a much easier way to add this data type together in VBA without having to write formula on the fly? It doesn't really matter because I have a working macro, but I just have a feeling that there's a much easier way to approach this that I'm over looking without having to use so much code.

Thanks,

Tom
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
The reason for this result is that the cells with time values are actually TEXT strings (and are not actual time values).
Although it says General it is stored as TEXT data.
Easiest way to check for string that look as values is to clear the Font alignment. Text is Left Aligned, Numbers - Right.
You can use this:

Code:
[COLOR=#333333]myVar = Timevalue(wbInb.Sheets("Inbound SLA").Range("O" & raRow).Value) + _[/COLOR]
[COLOR=#333333]Timevalue(wbInb.Sheets("Inbound SLA").Range("Q" & raRow).Value)[/COLOR]
 

tomhoneyfield

New Member
Joined
Jul 19, 2012
Messages
3
The reason for this result is that the cells with time values are actually TEXT strings (and are not actual time values).
Although it says General it is stored as TEXT data.
Easiest way to check for string that look as values is to clear the Font alignment. Text is Left Aligned, Numbers - Right.
You can use this:

Code:
[COLOR=#333333]myVar = Timevalue(wbInb.Sheets("Inbound SLA").Range("O" & raRow).Value) + _[/COLOR]
[COLOR=#333333]Timevalue(wbInb.Sheets("Inbound SLA").Range("Q" & raRow).Value)[/COLOR]

I can't believe I didn't think of TimeValue to be honest, I was just using DateValue the other day as well. Thanks for that.

Tom
 

Watch MrExcel Video

Forum statistics

Threads
1,109,380
Messages
5,528,366
Members
409,817
Latest member
JiNXX9500

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top