VBA code for calculating time

James__S

Active Member
Joined
Jan 26, 2014
Messages
332
Hi
I select a cell and press the button and it inserts date and time in cell
I insert the start date and time in the ranges for F Column
I then insert a finish date and time in the range for G Column

I use this VBA code below with an Active X Control button
Code:
Private Sub CommandButton1_Click()


ActiveCell = Now()


End Sub

1. How do i limit the code to only use the following ranges
F5:F32 & G5:G32
F35:F60 & G35:G60
2. If there is a start date and time in say cell F5 and a finish date and time in G5 can cell H5 then calculate the difference in minutes
Example: [start cell F5] 01-Jan-2018 04:22 [finish cell G5] 03-Jan-2018 14:52 [H5 - minutes between the 2 dates]
H5 will be blank unless F5 & G5 have a date and time
Is it possible to add these 2 requests to the code above

Thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi, for 1 you could try something like:

Code:
Private Sub CommandButton1_Click()
If Not Intersect(ActiveCell, Range("F5:G32,F35:G60")) Is Nothing Then
  ActiveCell = Now()
End If
End Sub

For 2 you could pre-populate those cells with a formula, for example, in H5.

=IF(COUNT(F5:G5)=2,(G5-F5)*24*60,"")
 
Upvote 0
Thanks FormR
Works well

Can the following be added to the code
Once an entry is inserted in say F5 & G5 and H5 is calculated can these 3 cells be locked so they cannot be changed
This would be for the entire range as cells have entries

Thanks
 
Upvote 0
Once an entry is inserted in say F5 & G5 and H5 is calculated can these 3 cells be locked so they cannot be changed

Hi, You would need to initially set the locked property of the cells you want to allow to be changed to false and protect the worksheet. Then in your code you would need to unprotect the sheet, set the locked property of the cells in question to True and then re-protect the sheet afterwards.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

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