Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Apr 2015
    Posts
    154
    Thanks
    70
    Thanked 0 Times in 0 Posts

    Change VBA code to allow both string and integer to be entered into a text box

    Experts-

    I am studying an Excel form. The code below requires the user to put a number in TextBox1 (IsNumeric). I would like to learn how to code TextBox1 so that it accepts both letters and numbers.

    Initially, I thought I could change IsNumeric to IsString to accomplish using letters and numbers. Unfortunately, that didn't work out. I have many hours working on this and decided I had better bring it here to find the answer.

    Thanks in advance for any help.



    Option Explicit


    Dim id As Integer, i As Integer, j As Integer, flag As Boolean


    Sub GetData()


    If IsNumeric(UserForm1.TextBox1.Value) Then
    flag = False
    i = 0
    id = UserForm1.TextBox1.Value

    Do While Cells(i + 1, 1).Value <> ""

    If Cells(i + 1, 1).Value = id Then
    flag = True
    For j = 2 To 3
    UserForm1.Controls("TextBox" & j).Value = Cells(i + 1, j).Value
    Next j
    End If

    i = i + 1

    Loop

    If flag = False Then
    For j = 2 To 3
    UserForm1.Controls("TextBox" & j).Value = ""
    Next j
    End If

    Else
    ClearForm
    End If


    End Sub

  2. #2
    Lounger
    Join Date
    Feb 2011
    Posts
    28
    Thanks
    0
    Thanked 10 Times in 8 Posts
    If I understand what you're after, since a textbox will accept any input by default, I think that you just need to remove the checking for numbers only, so you'll need to declare id as a String and remove the
    Code:
    If IsNumeric(UserForm1.TextBox1.Value) Then
    and
    Code:
    Else
    ClearForm
    End If
    from the code and that should fix that for you.
    Last edited by unclehewie; 2015-10-26 at 18:46.

  3. The Following User Says Thank You to unclehewie For This Useful Post:

    Excelnewbie (2015-10-27)

  4. #3
    2 Star Lounger
    Join Date
    Apr 2015
    Posts
    154
    Thanks
    70
    Thanked 0 Times in 0 Posts
    unclehewie-

    Thank you for your help with this code. Your approach answered my question. Thank you.

Posting Permissions

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