Thread: textboxes
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default textboxes

Hi Libby,

I assume you mean userform textboxes?

Here is a textbox event that only allows numeric input

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case 32 ' space
Case Else
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
Beep
End Select

End Sub

It also allows spaces, if you don't want that remove the line

Case 32 ' space

The simplest way to do the calc if you know the worksheet functions is to
use evaluate, for example

Evaluate("Average(1,2,3)/Round(123.456,1)")

although it is better to code it if you can.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"libby" wrote in message
...
Hi
There are two parts to this question.

1.
Is there any way I can make it so that only a numeric
value (preferrably within certain limits) can be entered
into a textbox?

2.
If the above is possible, then I have 3 textboxes which
are used to perform a calculation, the results of which
will be displayed in a label.
I know what the formula is on an excel sheet, using the
functions Average() and trunc(), but how would I code this
in vba?

Any help much appreciated