accepting numbers only in user forms (VBA)

VBAmalta89

Board Regular
Joined
Mar 25, 2011
Messages
116
I am doing some data validation on my user forms in VBA. I want the text boxes i have available to accept only numbers.

I have managed to do this for normal text boxes, however, I cannot do it for text boxes that lie within a frame. The line of code I am using is as follows for the normal text boxes:

Private Sub DisposeWood_Change()

OnlyNumbers

End Sub

Private Sub OnlyNumbers()

If TypeName(Me.ActiveControl) = "TextBox" Then

With Me.ActiveControl

If Not IsNumeric(.Value) And .Value <> vbNullString Then

MsgBox "Sorry, only numbers allowed"

.Value = vbNullString

End If

End With

End If

End Sub

How do i alter this to only accept numbers in text boxes which are inside frames?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I think the problem is because you are using ActiveControl which is a property of both a form and a frame

This might illustrate what I mean.
Code:
Private Sub TextBox1_Change()

    MsgBox Me.ActiveControl.Name

    If TypeName(Me.ActiveControl) = "Frame" Then
        MsgBox Me.Frame1.ActiveControl.Name
    End If

End Sub

It Textbox1 is in a frame you'll get 2 messages,, if it isn't you'll only get one.
 
Upvote 0
It's not a solution, I was trying to explain why the code you posted wasn't working.

Apologies.:oops:

I would actually recommend using entirely different code to check for numeric.

That's mainly because, as you've seen, you can't rely on using ActiveControl.

If you don't have too many textboxes you could add a simple check in each one's Exit event.

Something like this perhaps.
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

       If Not IsNumeric(TextBox1.Value) Then
               MsgBox "Only numbers allowed"
               Cancel = True
      End If
 
End Sub
There's probably quite a few other options to do validation.
 
Upvote 0
ok so this seems to be working, however it is prompting mw with the message once i exit the user form.

Is there a way i can make it prompt once a letter is entered instead of a number in a text box?
 
Upvote 0
A particular letter or character?

You could check that 1 character has been entered using Len but if you do want to restrict that to A-Z or something similar it would be slightly more complicated.

If did want that then perhaps you could try a combobox which only has the allowed characters listed?

The user could still type the value in and you could can easily check what they've typed is on the list.
 
Upvote 0
All characters must be rejected and numbers only need to be accepted.

I am new to VBA so i would appreciate if you could explain what you meant in your previous message more clearly.
 
Upvote 0
Oops, my bad.:oops:

I though you wanted a character to be entered, not a number.

Using IsNumeric will reject anything entered that has a character.

If you want to check every character as it's entered then you'll find code for that on the board.
 
Upvote 0
Hi,

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
    Case 46, 48 To 57
    Case Else
        KeyAscii = 0
        MsgBox "Only numbers allowed"
End Select
End Sub

Remove 46 if you don't need a decimal.

HTH
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,681
Members
452,937
Latest member
Bhg1984

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