简体   繁体   English

使用表格从excel表格中读取数据

[英]Using a form to read data from a table in excel

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.我想要这个的原因是它使用户更容易阅读 Excel 表格中的大量信息。

I have added the below screenshot of a quick example.我添加了一个快速示例的以下屏幕截图。

在此处输入图像描述

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. 1、2 或 3,然后下面的列表显示某些单元格信息。 So for example if the user selects ID 2, the Task box should read "Make Dinner" and the outcome box should read "Burnt"因此,例如,如果用户选择 ID 2,则任务框应显示为“制作晚餐”,结果框应显示为“烧毁”

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. :) 谢谢。

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.当然,您需要将TextBox1ListBox1修改为列表和文本框的适当名称。 Then, for ListBox1.List(ListBox1.ListIndex,0) set 0 to the correct column you want the textbox to match.然后,对于ListBox1.List(ListBox1.ListIndex,0)0设置为您希望文本框匹配的正确列。 0 Would be the first column, 1 the second, and so on. 0 表示第一列,1 表示第二列,以此类推。 ListIndex refers to the currently selected list item, so that does not require change. ListIndex 指的是当前选择的列表项,因此不需要更改。

You need an event that runs every time the user clicks within the ListBox, this is entered into the Userform's code window:您需要一个每次用户在 ListBox 中单击时运行的事件,该事件被输入到 Userform 的代码窗口中:

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.然后,您可以使用ListBox1.Value对用户单击的“行”做出反应 - 请注意,我的 ListBox 称为 ListBox1,您的可能是别的东西。

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.同样,我的框被称为Task1Outcome1 - 将它们更改为您的命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM