VBA lookup i can only enter numbers and not letters please help

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,231
Office Version
  1. 2010
Platform
  1. Windows
Hi i have the code below where it does a search but it will only let me enter numbers in the textbox and not letters, but i want to be able to input both numbers and letters can you help please, i am very new to VBA and still learning, i have searched for the answer alot on internet with no success so far, really hope you can help me.
Code:
Sub GetData()

If IsNumeric(UserForm1.TextBox1.Value) Then
    flag = False
    i = 0
    id = UserForm1.TextBox1.Value
    
    Do While ThisWorkbook.Worksheets("Sheet2").Cells(i + 1, 1).Value <> ""
    
        If ThisWorkbook.Worksheets("Sheet2").Cells(i + 1, 1).Value = id Then
            flag = True
            For j = 2 To 3
                UserForm1.Controls("TextBox" & j).Value = ThisWorkbook.Worksheets("Sheet2").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
 
Last edited by a moderator:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
The first line of code is testing to see if the value in textbox1 is numeric.
Try removing/changing that line
 
Upvote 0
Hi there, thanks for gettting back to me, i have tried deleting the line which does allow me to enter letters and numbers, but doing this it doesnt search anymore and i get mutiple errors. Is there any way to add in the code for both numberic and text? i dont know how to do this, thanks for your time :)
 
Upvote 0
How about
Code:
Sub GetData()

If Len(UserForm1.TextBox1.Value) > 0 Then
    flag = False
    i = 0
    ID = UserForm1.TextBox1.Value
    
    Do While ThisWorkbook.Worksheets("Sheet2").Cells(i + 1, 1).Value <> ""
    
        If ThisWorkbook.Worksheets("Sheet2").Cells(i + 1, 1).Value = ID Then
            flag = True
            For j = 2 To 3
                UserForm1.Controls("TextBox" & j).Value = ThisWorkbook.Worksheets("Sheet2").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
 
Upvote 0
Hi I get another error come up on line below:
id = UserForm1.TextBox1.Value
I can enter text though but the above error shows up, thank you for your patience on this :)
 
Upvote 0
sorry not sure i copied and [pasted the code you provided sorry i tried changing it to (if and i) but that didnt work :)
thank you again in your help :)
 
Upvote 0
i think the Id is my userform1 where it searches to get the data from
 
Upvote 0
I dont know ID was added in the prev code
If you don't know, then I don't know, which makes it almost impossible to help.
Can you supply the rest of the code?

sorry not sure i copied and [pasted the code you provided sorry i tried changing it to (if and i) but that didnt work :)
thank you again in your help :)
You've completely lost me on this. Can you please re-explain?
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,856
Members
449,194
Latest member
HellScout

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