Date/time issues! VBA.

Crazzled

New Member
Joined
Oct 4, 2013
Messages
7
So, I've made a small loop formula to process some data;

Sub testtosortdata()
Rows(1).Delete
Range("N:O").NumberFormat = "hh:mm:ss"
Dim counter As Integer
counter = 1
Do Until ThisWorkbook.Sheets("sheet4").Cells(counter, 14).Value = ""
'set loop with empty cell ending
Dim value1 As Date
value1 = Range("N" & (counter + 1)).Value
Dim value2 As Date
xvalue2 = Range("ON" & counter).Value
Dim value3 As Date
value3 = value2 - value1
ThisWorkbook.Sheets("sheet4").Range("O" & counter).Value = value2 - value1
counter = counter + 1
Range("O").NumberFormat = "hh:mm:ss"
Loop


End Sub

It works off an imported table, and once the row is deleted is it dates.

I have a small issue.

value1 is a date, and time.
value 2 is a date, and time.

I want to copy over the time only, but so far despite whatever formatting I seem to throw at it, it either makes the value it as -4xxxxxx.xxx, depending on the date.

I basically need to work out the different in minutes between the two times, ignorant of date since the dates will always be the same day.

Any ideas?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Is this really in your code?

Rich (BB code):
xvalue2 = Range("ON" & counter).Value

Do you have Option Explicit set? if not I strongly recommend that you do.

To extract the time from a date and time:

Rich (BB code):
value1 = Range("N" & (counter + 1)).Value - Int(Range("N" & (counter + 1)).Value)

or, with the cell formatted as time only:

Rich (BB code):
value1 = TimeValue(Range("N" & (counter + 1)).Text)
 
Upvote 0
You just need to multiply the result by 1440 to get a value in minutes:

Code:
ThisWorkbook.Sheets("sheet4").Range("O" & counter).Value = (value2 - value1)*1440

And format this cell using a number (rather than date) format
 
Upvote 0
Thank you both!
Yeah, the names aren't too relevant - I was just trying to work out what would work, before I added it into my worksheet they will be defined with a bit more time when I write the actual bit in. I wasn't using option explicit, and I'm not even sure what it was but after a quick look into it, seems it would be best to have it on.
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,778
Members
449,336
Latest member
p17tootie

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