Time calculations, with a twist.

jag108

Active Member
Joined
May 14, 2002
Messages
433
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hi,

I am trying to calculate the start time + duration to get next start time.

I have two columns column B has the start time, column "C" has the duration, I would like these to be added together to come up with the next start time.

This is a cross reference sheet to make sure that the data being imported is correct.

One thing I need to add is we work on a clock of 06:00 to 29:59 so a 30 hour clock, this is a broadcasting thing so don't get me started :)

My code looks like this.

Sub check_import()
Dim i As Integer, start_time, next_start_time, total_time As Date

On Error GoTo errortrap
vbyes_no = MsgBox("Please select the sheet you wish to run this code on. " & Chr(13) & _
"Is the correct sheet selected [Y/N]", vbInformation + vbYesNo, "Select the correct sheet.")
If vbyes_no = vbNo Then End
Set TestRange = Intersect(Range("E:E"), ActiveSheet.UsedRange)
lastrow = TestRange.Cells(TestRange.Cells.Count).Row 'get last row number in the range
firstrow = TestRange.Cells(1).Row 'get the first row number in the range
For i = 2 To lastrow Step 1
start_time = Cells(i, 2).Value
next_start_time = Cells(i + 1, 2).Value
duration = Cells(i, 3).Value
total_time = start_time + duration
If Not total_time = next_start_time Then
MsgBox "Cell " & Cells(i, 2).Address & " has the wrong duration."
End If
Next i
Exit Sub
errortrap:
MsgBox "An error has occured stopping this process form completing." & Chr(13) & _
"Error number: " & Err.Number & Chr(13) & "Error Description: " & Err.Description, vbExclamation + vbOKOnly, "An error has occured!"
End Sub


Any help really appreciated
 
Hi Jag108:

I don't understand your constraints ... so please post a screen shot of ...

1) your raw data before a macro is executed

2) your data after the macro is executed

3) how will the macro be executed?
a) through a command button
b) a worksheet event
c) ...
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
1. You have a screen shot of the data, that will not change, and cannot change. There can only be raw data because the import program does not like any thing else. This is why we hvae to make sure the data is correct before importing.

2. The data will only be changed by the user after the macro indicates that there is an issue with a particular event.

3. There will be a button on the standard excel toolbar, also created by VBA.

I will have the macro halt on the row where there is an issue, so that the user can investigate the problem. So it may need to be run multiple times to complete
 
Upvote 0
Hi Jag108:

It appears that you only need to check that the StartTime entries are in your 30 Hour Recording cycle ... to automate it, you may want to run a macro on activating the sheet that ensures that each StartTime entry meets your desired criterion.
 
Upvote 0
Yes my problem is getting the code that will do this.

All the variable combinations are treating the values as strings.

I need the cell values to be interpreted as either integers or some other numeric value.

I have tried a number of different methods but this is not happening.
 
Upvote 0
Hi jag108:

Let me try again to see if the following will work for you ...

Let us say my data for StartupTime in cell B2 down is blank, is incorrect, or does not meet your 30 hour cycle ...
y051124h1.xls
ABCD
101-Dec-0518:000:30New American Sportsman
201-Dec-0519:000:30Fishing
301-Dec-0520:000:30Around the Horn
401-Dec-0521:000:30Pardon the Interruption
501-Dec-051:00Sportscenter LIVE
601-Dec-0523:000:30ATP Tennis Weekly
701-Dec-0524:000:30Pardon the Interruption
801-Dec-051:000 Ice Skating
901-Dec-052:300 Golf
1001-Dec-050:300 Pardon the Interruption
1101-Dec-052:000 Sportscenter
1201-Dec-051:300 Drag Racing
1301-Dec-0530:000:300 NFL Films Presents
1402-Dec-0531:000:30New American Sportsman
1502-Dec-0532:000:30Motorsport Asia
1602-Dec-050:30Diving Passport
1702-Dec-050:30ATP Tennis Weekly
1802-Dec-050:30ESPN Fitness Pros
1902-Dec-0536:000:30Anthology
Sheet1


then after executing the following code ...
Code:
Private Sub CommandButton1_Click()
    For Each cell In Range("B2:B" & [a2].End(xlDown).Row)
        cell.Select
        cell.Value = cell(0, 1) + cell(0, 2)
        Debug.Print cell.Value * 24
        cell.Value = (cell.Value * 1440 Mod 1800) / 1440
        Debug.Print cell.Value
        If cell.Value< 0.25 Then cell.Value = cell.Value + 0.25
    Next cell
End Sub
my data in cells B2 down is made to comply with your requirements ...
y051124h1.xls
ABCD
101-Dec-0518:000:30New American Sportsman
201-Dec-0518:300:30Fishing
301-Dec-0519:000:30Around the Horn
401-Dec-0519:300:30Pardon the Interruption
501-Dec-0520:001:00Sportscenter LIVE
601-Dec-0521:000:30ATP Tennis Weekly
701-Dec-0521:300:30Pardon the Interruption
801-Dec-0522:001:000 Ice Skating
901-Dec-0523:002:300 Golf
1001-Dec-0525:300:300 Pardon the Interruption
1101-Dec-0526:002:000 Sportscenter
1201-Dec-0528:001:300 Drag Racing
1301-Dec-0529:300:300 NFL Films Presents
1402-Dec-0506:000:30New American Sportsman
1502-Dec-0506:300:30Motorsport Asia
1602-Dec-0507:000:30Diving Passport
1702-Dec-0507:300:30ATP Tennis Weekly
1802-Dec-0508:000:30ESPN Fitness Pros
1902-Dec-0508:300:30Anthology
Sheet1


I hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,680
Members
449,116
Latest member
HypnoFant

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