How do I ignore text when calculating time?

Moonbeam111

Board Regular
Joined
Sep 24, 2018
Messages
97
Office Version
  1. 365
  2. 2010
I ranges E41:E49 i have times:


0:20 minutes
0:48 minutes
0:13 minutes
0:33 minutes
0:22 minutes
0:10 minutes
0:00 minutes
0:00 minutes
1:29 minutes


I have a function here that I call on:

VBA Code:
Function CalcHrsMins(TotalMinutes As Variant) As Variant
Dim varHours, varMinutes
'calculate the hours
varHours = Int(TotalMinutes / 60)
'calculate the remaining minutes
varMinutes = Format(TotalMinutes - (varHours * 60), "00")
'return the combined hours and minutes
CalcHrsMins = varHours & ":" & varMinutes
End Function


VBA Code:
Sub test()
Dim varTotHours As Variant
Dim varHrsMinutes As Variant
Dim time As Range
For Each time In Range("E41:E49")
varTotHours = time
varHrsMinutes = CalcHrsMins(varTotHours)
time = varHrsMinutes
Next time
End Sub

But it doesnt work if I call it while I have the "minutes" text in the range of cells. It just automatically does this:


minutes
minutes
minutes
minutes
minutes
minutes
minutes
minutes
minutes

Id like to keep the text and number in the same cell. Does anyone have the knowledge necessary to know how to fix this? If its possible at all?
 
Do not keep text and number in the same cell. This is not a good practice for reasons you are discovering. I suggest you remove the text " minutes" from your cells, and leave it as a true time value. Then you can format the cells using custom format

m:ss "minutes"

Then you don't have to make any changes to your code, although I have not tested your code to confirm that it works.

1740063845218.png
 
Upvote 0
That doesn't appear to work with the code I have. If i put it like this in the custom cell field: h "hours" m "minutes". The numbers remain as 0.
 
Upvote 0
It appears to take a number of minutes and format it as h:mm in a string. But if you use time values to start with there is no reason to do that. What's the overall problem you are trying to solve?
 
Upvote 0
I have a macro that gives me a certain randomized number of minutes and it automatically adds them to one of the cells in range E41:E49 based off a vlookup on the cell to the left.

VBA Code:
Set rfound = Range("D41:D49").Find(What:=Range("H22"))
rfound.Offset(0, 1) = rfound.Offset(0, 1) + Range("A73")

I am trying to format it in the format h "hours" m "minutes". For example if it is "20 minutes" show up as 0 hours, 20 minutes. If the cell was 90 minutes, show up as "1 hour 30 minutes". I can do that using the special custom format. h "hours" m "minutes", however if I run the code like that all of the cells in that range change to "0 hours 0 minutes."

I might be getting the problem because the code is trying to add the numbers and text together. And whenever I click the cell it shows up as 12:00:00 AM in the formula bar.
 
Upvote 0
I don't see where you are getting a randomized number of minutes--is that's what's in A73? What is in H22? What is in D41:D49?

If you have time values in E41:E49 as I suggest, and A73 is an integer number of minutes, then you don't need the code you posted in #1 at all. You can modify the code above to this:

Rich (BB code):
Set rfound = Range("D41:D49").Find(What:=Range("H22"))
rfound.Offset(0, 1) = rfound.Offset(0, 1) + Range("A73")/24/60
 
Upvote 0
Solution

Forum statistics

Threads
1,226,812
Messages
6,193,116
Members
453,777
Latest member
Miceal Powell

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