SpinButton Rounding Up

R

Randal W. Hozeski

I use the following code in a SpinButton. If I manually edit the TextBox1
to 150.25 it is fine but when I use150.6, it rounds the result up and does
not allow the two decial point entry in fact any number over 150.5 turns
to 151. How can I keep the number entered as is without rounding it
up? Not sure which property needs adjusting,

Private Sub SpinButton1_Change()
TextBox1.Text = SpinButton1.Value
End Sub

Private Sub TextBox1_Change()
Dim NewVal As Integer

NewVal = Val(TextBox1.Text)
If NewVal >= SpinButton1.Min And _
NewVal <= SpinButton1.Max Then _
SpinButton1.Value = NewVal
End Sub

-Randy-
 
R

Rob van Gelder

I've skipped the min/max validation, but this approach (or similar) may work
for you:

Private dblMyValue As Double

Private Sub SpinButton1_SpinUp()
dblMyValue = dblMyValue + SpinButton1.SmallChange
TextBox1.Text = dblMyValue
End Sub

Private Sub SpinButton1_SpinDown()
dblMyValue = dblMyValue - SpinButton1.SmallChange
TextBox1.Text = dblMyValue
End Sub

Private Sub TextBox1_Change()
dblMyValue = Val(TextBox1.Text)
If dblMyValue >= SpinButton1.Min And _
dblMyValue <= SpinButton1.Max Then _
SpinButton1.Value = dblMyValue
End Sub
 
R

Randal W. Hozeski

This keeping crashing on me @ the following

Private Sub TextBox1_Change()
dblMyValue = Val(TextBox1.Text)
If dblMyValue >= SpinButton1.Min And _
dblMyValue <= SpinButton1.Max Then _
SpinButton1.Value = dblMyValue
End Sub

and is not working? The up/down arrow are not
spinning anymore. any idea why?
My code is working fine, scrollwise I just need
it to stop rounding up.
-Randy-
 
R

Rob van Gelder

That's strange.

I've just created a new workbook, added a userform, added a spinbutton,
added a textbox, copied the code from my last e-mail and ran the form (by
pressing F5)
Worked OK here.

What's you error message?
 
R

Randal W. Hozeski

Thanks for the follow up.
I guess that crash is a bad word to use.

When I use your example. It does allow my to use the textbox to
manually change the value, and it does not roundup. (what I requested)
but it does not allow my to use the up/down arrows of the SpinBox. It
just jumps between 1 to -1.

My goal here is to display to the hundredth and increment by .25 When I
created the Spinbutton and double clicked on it to enter the code I only
get a
Private Sub SpinButton1_Change()
which I then replaced with your code. (SpinButton1_SpinUp and SpinDown)
I doubled on the textbox and added the other.

It appears to be close. I just need to get the SpinButons to work.

-Randy-
 
T

Tom Ogilvy

Private Sub SpinButton1_SpinUp()
If Len(Trim(TextBox1.Text)) = 0 Then _
TextBox1.Text = 0
TextBox1.Text = Format(CDbl(TextBox1.Text) + 0.25, "0.00")
SpinButton1.Value = 0
End Sub




Private Sub SpinButton1_SpinDown()
If Len(Trim(TextBox1.Text)) = 0 Then _
TextBox1.Text = 0

TextBox1.Text = Format(CDbl(TextBox1.Text) - 0.25, "0.00")
SpinButton1.Value = 0

End Sub

Worked for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top