manipulating time in VBA

amitshah

Board Regular
Joined
Apr 13, 2002
Messages
80
Hi I need to add time to a variable in VBA. I have tried every possible variable because what I need to do is to first convert that value into some form where I can add or delete time ie in minutes or hours and then I need to convert it back to time value.

I do this by using VBA.TimeValue(String/Date)

But from here I dont know how to manipulate ie add or deleite time from the above if I assign it to some variable

How can I do this? in VBA

Thanks in advance.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You just need to dimension your variable as Date, which is stored as IEEE 64-bit (8-byte) floating-point numbers that represent dates ranging from 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59. Values to the left of the decimal represent date information while values to the right of the decimal represent time. Midnight is 0 and midday is 0.5. Negative whole numbers represent dates before 30 December 1899.

Then you can perform simple arithmentic, eg:

Code:
Sub Test()
    Dim MyDate As Date
    MyDate = TimeValue("08:35:17")
    MsgBox WorksheetFunction.Text(MyDate, "[hh]:mm:ss")
    MyDate = MyDate + TimeValue("12:12:12")
    MsgBox WorksheetFunction.Text(MyDate, "[hh]:mm:ss")
    MyDate = MyDate + 0.5
    MsgBox WorksheetFunction.Text(MyDate, "[hh]:mm:ss")
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,924
Members
448,533
Latest member
thietbibeboiwasaco

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