restriction on a number field

G

Grace

Can I set a number field to only allow numbers entered in
tenths. For example, I do not want a user to be able to
enter 1.75 and have it round up to 1.8. I need to
restrict the entry to only 1 decimal point.
 
D

Dave Peterson

You could restrict the entry by using Data|Validation
Custom
and a formula like:
=((A1*10)=INT(A1*10))

You could also have an event macro just do the rounding without asking.

Rightclick on the worksheet tab that should have this behavior. Select view
code. Paste this into the code window:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Me.Range("a1")) Is Nothing Then Exit Sub

On Error GoTo errHandler:
With Target
If IsNumeric(.Value) Then
Application.EnableEvents = False
.Value = Application.RoundUp(.Value, 1)
End If
End With

errHandler:
Application.EnableEvents = True

End Sub


Adjust A1 to the range you want to check.
 

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