Results 1 to 6 of 6

vba- loop for only numbers in inputbox

This is a discussion on vba- loop for only numbers in inputbox within the Excel Questions forums, part of the Question Forums category; hello i want to create inputbox that the excel user can only type only numbers and must put numbers (8 ...

  1. #1
    New Member
    Join Date
    May 2012
    Posts
    28

    Default vba- loop for only numbers in inputbox

    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?

  2. #2
    Board Regular
    Join Date
    Dec 2011
    Posts
    3,638

    Default Re: vba- loop for only numbers in inputbox

    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)
    For testing your Regular Expression patterns, try my Regular Expression Pattern Testing add-in!

    For notes on how to use it - see here.

  3. #3
    New Member
    Join Date
    May 2012
    Posts
    28

    Default Re: vba- loop for only numbers in inputbox

    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?

  4. #4
    Board Regular
    Join Date
    Dec 2011
    Posts
    3,638

    Default Re: vba- loop for only numbers in inputbox

    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.
    For testing your Regular Expression patterns, try my Regular Expression Pattern Testing add-in!

    For notes on how to use it - see here.

  5. #5
    New Member
    Join Date
    May 2012
    Posts
    28

    Default Re: vba- loop for only numbers in inputbox

    thanks , but agaih- how to restrict the textbox on the form to only numbers and 8 numbers?

  6. #6
    Board Regular
    Join Date
    Dec 2011
    Posts
    3,638

    Default Re: vba- loop for only numbers in inputbox

    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
    For testing your Regular Expression patterns, try my Regular Expression Pattern Testing add-in!

    For notes on how to use it - see here.

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com