Textbox Editable

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
784
Office Version
  1. 365
Hi,

I have a listbox how can make the textbox able to edit the text on the form the code i have is this:


VBA Code:
Private Sub ListBox_Results_Click()
'Go to selection on sheet when result is clicked

Dim strAddress As String
Dim l As Long

    For l = 0 To ListBox_Results.ListCount
        If ListBox_Results.Selected(l) = True Then
            strAddress = ListBox_Results.List(l, 1)
            ActiveSheet.Range(strAddress).Select
            'Populate textboxes with results
            With ActiveSheet
            
                COGSform_All.TextBox_Results1.Value = .Cells(.Range(strAddress).Row, 2).Value
                COGSform_All.TextBox_Results2.Value = .Cells(.Range(strAddress).Row, 3).Value
                COGSform_All.TextBox_Results3.Value = .Cells(.Range(strAddress).Row, 13).Value
                COGSform_All.TextBox_Results4.Value = .Cells(.Range(strAddress).Row, 10).Value
                COGSform_All.TextBox_Results5.Value = .Cells(.Range(strAddress).Row, 14).Value
                COGSform_All.TextBox_Results6.Value = .Cells(.Range(strAddress).Row, 4).Value
                COGSform_All.TextBox_Results8.Value = .Cells(.Range(strAddress).Row, 5).Value
                COGSform_All.TextBox_Results9.Value = .Cells(.Range(strAddress).Row, 6).Value
                COGSform_All.TextBox_Results10.Value = .Cells(.Range(strAddress).Row, 11).Value
                COGSform_All.TextBox_Results11.Value = .Cells(.Range(strAddress).Row, 12).Value
                COGSform_All.TextBox_Results12.Value = .Cells(.Range(strAddress).Row, 8).Value
                COGSform_All.TextBox_Results13.Value = .Cells(.Range(strAddress).Row, 15).Value
                COGSform_All.TextBox_Results14.Value = .Cells(.Range(strAddress).Row, 16).Value
                COGSform_All.TextBox_Results15.Value = .Cells(.Range(strAddress).Row, 9).Value
                COGSform_All.TextBox_Results16.Value = .Cells(.Range(strAddress).Row, 7).Value
                COGSform_All.TextBox_Results17.Value = .Cells(.Range(strAddress).Row, 1).Value
            End With
            GoTo EndLoop
        End If
    Next l

EndLoop:
    
End Sub

Thank you,
 

Attachments

  • LISTBOX.PNG
    LISTBOX.PNG
    21.4 KB · Views: 16
Apparently, I should be asking the wrong questions, so you insist on not answering them. Therefore, I will try to provide a generic solution to your question. Please follow the instructions, then apply them to your own data range by adding more text boxes on the user form, and also updating the code to include the new text boxes.

The following is an example of how I would set up a simple project that will do the following as it should give an idea about how to set it up.
  • Populate the list box by using the first column of my source data.
  • Populate the text boxes when I click on an item in the list box accordingly.
  • Update the data in the source range when I change the text box values and click on the update button.
The following shows the finished project.
sample.gif


The following shows how to build it.
  1. Create a new workbook and copy and paste the following mini-sheet into Sheet1:
    Book1.xlsm
    ABC
    1Field AField BField C
    2A11-Oct10
    3B12-Oct20
    4C13-Oct30
    Sheet1
  2. Goto VBE and create a new user form with the following controls. All controls have their default names. ListBox1, TextBox1, TextBox2, TextBox3, and CommandButton1.
    1620671292188.png
  3. Open the UserForm1 module, delete everything in it (in case it has Option Explicit line) and copy and paste the following code:
    VBA Code:
    Option Explicit
    Dim rng As Range
    
    Private Sub UserForm_Initialize()
    Dim cll As Range
        ' Populate the list box
        ' Assuming column A is the lookup range
        Set rng = Sheet1.Range("A2:A4")
        For Each cll In rng.Rows
            ListBox1.AddItem cll.Value
        Next cll
    End Sub
    
    Private Sub ListBox1_Click()
    Dim cll As Range
        ' Find the lookup value in the data range
        Set cll = rng.Columns(1).Find(ListBox1.Value, rng.Cells(1, 1), xlValues)
        TextBox1.Value = cll.Cells(, 1)
        TextBox2.Value = cll.Cells(, 2)
        TextBox3.Value = cll.Cells(, 3)
    End Sub
    
    Private Sub CommandButton1_Click()
    Dim cll As Range
        ' Find the selected list item value in the data range
        Set cll = rng.Columns(1).Find(ListBox1.Value, rng.Cells(1, 1), xlValues)
        ' First column is the lookup range, so we don't update it but the others
        cll.Cells(, 2).Value = TextBox2.Value
        cll.Cells(, 3).Value = TextBox3.Value
    End Sub

Run and see how it works. You can then follow the code and adapt to your own project easily.
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Worked perfectly.

Thank you So much for all your help.
You're welcome. Glad to hear it helped.

Please mark the post as the solution by using the checkmark button right next to that post in this and future questions that you received a working answer. This way, future readers can find this answer easier.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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