[VBA] Adding a number to time with custom formatting

mrspz

New Member
Joined
Nov 9, 2017
Messages
37
At the moment I have the following

Code:
Private Sub SpinButton2_SpinUp()   
TextBox_starttime.Value = Format(CDate(TextBox_starttime.Value) + 1 / 96)
End Sub

Which for the most part works fine, what's driving me crazy is that I have the textbox formatted as "13:00", however when I click the up arrow it changes the formatting when it carries out the addition. So it comes out as "01:15:00 PM" while I want it to appear simply as "13:15".

I've looked everywhere and have yet to find a solution.

Any help greatly appreciated.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi
Welcome to the board

You forgot the formatting string in the Format() function.

Code:
Private Sub SpinButton2_SpinUp()
TextBox_Starttime.Value = Format(CDate(TextBox_Starttime.Value) + 1 / 96[B][COLOR=#b22222], "hh:mm"[/COLOR][/B])
End Sub
 
Upvote 0
Just a remark (doesn't change the result), in this case I'd use time functions, I think it would be easier to read:

Code:
TextBox_Starttime.Value = Format(TimeValue(TextBox_Starttime.Value) + TimeValue("00:15"), "hh:mm")
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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