Home > Net >  Using a form to read data from a table in excel
Using a form to read data from a table in excel

Time:07-20

I cannot find the correct question to ask regarding this? So I though I would just quickly ask here. I'm trying to create a form that will allow a user to select a row from a table and show the information from specific cells in that table. reason I want this was is that it make reading a lot of info in a table in excel easier to users.

I have added the below screenshot of a quick example.

enter image description here

I want the user to be able to select a row ie. 1, 2 or 3 and then the lists below show certain cell information. So for example if the user selects ID 2, the Task box should read "Make Dinner" and the outcome box should read "Burnt"

I eventually want to add the capability for the user to edit the Task and Outcome and then have a button to save it over the original data.

This isn't my original file, as that contains confidential information. The above is just an example, that should help me get started.

Hopefully this makes sense. :) Thank you.

CodePudding user response:

You need an event that runs every time the user clicks within the ListBox, this is entered into the Userform's code window:

Private Sub ListBox1_Change()
    'do something with ListBox1.Value
End Sub

You can then react to which 'row' the user has clicked, using the ListBox1.Value - note, my ListBox is called ListBox1, yours might be something else.

You can use that value (row number) to locate the values to place in the Task and Outcome boxes.

Then you just write to the boxes, like so:

Me.Task1.Value = <value you want here>
Me.Outcome1.Value = <value you want here>

Again, my boxes are called Task1 and Outcome1 - change them to your naming convention.

CodePudding user response:

So, if you simply want to set a textbox to the value from a listbox, it would go as follows:

Private Sub ListBox1_Change()
TextBox1.Text = ListBox1.List(ListBox1.ListIndex, 0)
End Sub

Of course, you would need to modify TextBox1 and ListBox1 to the appropriate name of your list and text boxes. Then, for ListBox1.List(ListBox1.ListIndex,0) set 0 to the correct column you want the textbox to match. 0 Would be the first column, 1 the second, and so on. ListIndex refers to the currently selected list item, so that does not require change.

  • Related