[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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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