Userform data entry into excel

austin397

New Member
Joined
Dec 16, 2016
Messages
36
Hello all,

I am attempting to have my userform enter data into certain cells on my spreadsheet. I need it to enter the data from a text box into a cell. The cell that it goes into depends on the data selected from a list box.

So if in the list box I select the option of "8605", and in the text box is the data "52" then I need it to look at Column A and if it find a match in column A that is "8605", then enter "52" in the cell of the same row and in column D.

In this instance it would find "8605" in cell A193. So in cell D193 I need it to enter "52".

Please help!
 
Assuming your listbox is named "Listbox1"
Assuming your Textbox is named "Textbox1"
Modify names if needed.
Use this script:

Put this script in your listbox
When you click on a value in the listbox the script will run.

Code:
Private Sub ListBox1_Click()
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim c As Range
Dim ans As Long
ans = ListBox1.Value
For Each c In Range("A1:A" & Lastrow)
If c.Value = ans Then c.Offset(, 3).Value = TextBox1.Value
Next
End Sub
 
Last edited:
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
This one worked perfectly, thank you!

Assuming your listbox is named "Listbox1"
Assuming your Textbox is named "Textbox1"
Modify names if needed.
Use this script:

Put this script in your listbox
When you click on a value in the listbox the script will run.

Code:
Private Sub ListBox1_Click()
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim c As Range
Dim ans As Long
ans = ListBox1.Value
For Each c In Range("A1:A" & Lastrow)
If c.Value = ans Then c.Offset(, 3).Value = TextBox1.Value
Next
End Sub
 
Upvote 0
Not as fancy as Mr. Roderick, but it takes the bull by the horns

Code:
Private Sub CommandButton1_Click()
    Dim rowNbr As Long
    Dim foundItem As String
    If TextBox1.Text = vbNullString Then
        MsgBox "Please Populate Textbox"
        Exit Sub
    End If
    If searchFor = vbNullString Then
        MsgBox "Please Select Item from Listbox"
        Exit Sub
    End If
    Do
        rowNbr = rowNbr + 1
        If ActiveSheet.Cells(rowNbr, 1).Value = searchFor Then
            ActiveSheet.Cells(rowNbr, 4).Value = TextBox1.Text
            Exit Sub
        End If
    Loop While ActiveSheet.Cells(rowNbr, 1).Value <> vbNullString
End Sub
 
Upvote 0
Forgot this gem:

Code:
Private Sub ListBox1_Click()
    searchFor = ListBox1.Text
End Sub

This bulletproofs your code against users that do not select from the listbox.
 
Upvote 0
Thank you dssJones,

While I'm sure that your code does work, I couldn't quite get it to do what was desired. It didnt give any errors but it also didnt input the data from the textbox.

I thank you for your reply though!
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,108
Members
452,544
Latest member
aush

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