vba- loop for only numbers in inputbox

shmouel99

New Member
Joined
May 1, 2012
Messages
29
hello
i want to create inputbox that the excel user can only type only numbers and must put numbers (8 numbers like 12345678 or 22233670 and so on)
could you shoe a good code for this?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi

If you use Application.InputBox method rather than the Inputbox function, then you can restrict the return type to numbers only. You will still need to validate the number that is typed in, however (eg to be 8 digits without fractional part etc):

Code:
Dim myNum As Long
 
myNum = Application.InputBox("Enter your number",Type:=1)
 
Upvote 0
for diffrenet reason i want to use loop . i want to enter a message that if this is not number the user will get message that:

msgbox "please enter only numbers"


so how to do it?
and i didnt understant how to limit it to 8 digits?
 
Upvote 0
Then you need to use a textbox on a userform whereby you can utilise the textbox's Change event to restrict to no more than 8 characters and also to numeric digit entry only.
 
Upvote 0
Well, assuming you have your textbox on your userform and it is named TextBox1 (or amend the code to suit), you could use the following event code to restrict entry to digits only and to limit number of characters to no more than 8:

Code:
Private Sub TextBox1_Change()
With Me.TextBox1
    'check if 8 characters exceeded:
    If Len(.Text) > 8 Then
        .Text = Left(.Text, 8)
        Exit Sub
    End If
End With
End Sub
 
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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