Time text box with a spin button

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Time text box with a spin button

Post by adam »

Hi Anyone,


How do I get a spinner to increment time which is in a text box "txttime" in a userform in Microsoft Excel 2007?

I want the spin button to increase the value, when the up arrow is clicked; to decrease the value, when the down arrow is clicked. Typically, the user can also type a text value directly into the associated text box.

Can the following code be modified accordingly in replacement of percentage to time.

Thanks in advance.

Code: Select all

Private Sub SpinButton1_SpinDown()
txttime.Value = Format(0, "0.0")
txttime.Value = txttime.Value + SpinButton1.Value / 100
txttime.Value = Format(txttime.Value, "0.0%")
End Sub

Private Sub SpinButton1_SpinUp()
txttime.Value = Format(0, "0.0")
txttime.Value = txttime.Value + SpinButton1.Value / 100
txttime.Value = Format(txttime.Value, "0.0%")
End Sub
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Time text box with a spin button

Post by HansV »

Use code like this:

Code: Select all

Private Sub SpinButton1_SpinDown()
  Dim t As Date
  t = DateAdd("n", -1, TimeValue(Me.txtTime))
  Me.txtTime = Format(t, "hh:nn")
End Sub

Private Sub SpinButton1_SpinUp()
  Dim t As Date
  t = DateAdd("n", 1, TimeValue(Me.txtTime))
  Me.txtTime = Format(t, "hh:nn")
End Sub

Private Sub UserForm_Initialize()
  Me.txtTime = Format(0, "hh:nn")
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Time text box with a spin button

Post by adam »

Thanks for the help. Your code works the way as I had requested.
Best Regards,
Adam