ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA time calculation (https://www.excelbanter.com/excel-programming/435272-vba-time-calculation.html)

Nancy T.

VBA time calculation
 
I have created a command button. When I click it, I want it to get the
current time, add 16 hours to the current it, and enter that time in the
selected cell. I have spent way too much time trying to figure this out and I
am ready to pull my hair out. By the way, I am using Excel 2000. Any help
would be greatly appreciated!

muddan madhu

VBA time calculation
 
Private Sub CommandButton1_Click()
dt = Time
ActiveCell.Value = Format(dt + TimeValue("16:00:00"), "hh:mm:ss am/
pm")
End Sub


On Oct 22, 11:32*am, Nancy T.
wrote:
I have created a command button. When I click it, I want it to get the
current time, add 16 hours to the current it, and enter that time in the
selected cell. I have spent way too much time trying to figure this out and I
am ready to pull my hair out. By the way, I am using Excel 2000. Any help
would be greatly appreciated!



Rick Rothstein

VBA time calculation
 
This line of code should do what you want...

ActiveCell.Value = Time + TimeSerial(16, 0, 0)

You can, of course, format the cell to display this time in whatever format
you choose. Or you can set the NumberFormat property via code. For example
(to suppress the seconds using a 24-hour clock)...

ActiveCell.NumberFormat = "hh:mm"

--
Rick (MVP - Excel)


"Nancy T." wrote in message
...
I have created a command button. When I click it, I want it to get the
current time, add 16 hours to the current it, and enter that time in the
selected cell. I have spent way too much time trying to figure this out
and I
am ready to pull my hair out. By the way, I am using Excel 2000. Any help
would be greatly appreciated!



Sam Wilson

VBA time calculation
 

Activecell.Value = DateAdd("h", 16, Now())


"Nancy T." wrote:

I have created a command button. When I click it, I want it to get the
current time, add 16 hours to the current it, and enter that time in the
selected cell. I have spent way too much time trying to figure this out and I
am ready to pull my hair out. By the way, I am using Excel 2000. Any help
would be greatly appreciated!


Rick Rothstein

VBA time calculation
 
By the way, if you are only adding hours (even if decimal hours such as
14.25), then you can simplify this to the following...

ActiveCell = Time + 16/24

where you would replace the 16 by your decimal hours.

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
This line of code should do what you want...

ActiveCell.Value = Time + TimeSerial(16, 0, 0)

You can, of course, format the cell to display this time in whatever
format you choose. Or you can set the NumberFormat property via code. For
example (to suppress the seconds using a 24-hour clock)...

ActiveCell.NumberFormat = "hh:mm"

--
Rick (MVP - Excel)


"Nancy T." wrote in message
...
I have created a command button. When I click it, I want it to get the
current time, add 16 hours to the current it, and enter that time in the
selected cell. I have spent way too much time trying to figure this out
and I
am ready to pull my hair out. By the way, I am using Excel 2000. Any help
would be greatly appreciated!




Nancy T.

VBA time calculation
 
Thanks everyone! I knew I was making it more difficult than it needed to be.
(My only excuse is that it was about 1:00 AM by the time I threw in the towel
and came here!) These are all perfect.

Gratefully,
--Nancy




All times are GMT +1. The time now is 12:43 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com