0

我正在处理一个包含多个大表的电子表格,这些表的信息是通过我创建的基于电子表格的用户表单输入的,因为我不熟悉或不习惯使用 vba 用户表单,我正在尝试调用条目以进行编辑。

我能想到的最简单的方法是打开默认的 excel 数据表单。我需要知道的是;有没有办法为已经输入标准的特定表打开这个数据表单?

例如。我需要编辑产品表,所以我已经知道我正在编辑产品表并且我知道产品的名称。我有一个小用户表单,同样基于电子表格,我在其中选择条目类型(在本例中为产品),然后输入产品名称。我想要做的是使用该信息打开产品表的数据表单,其中已填写名称的条件字段,以减少搜索条目所花费的时间,因为这些表有数百个条目。

4

1 回答 1

1

对于这个简单的示例,我将您的工作表与产品一起称为“产品”。有两列,A 列中的名称和 B 列中的 ID

在用户窗体上,我放置了两个名为

  • 文本框_产品名称

  • TextBox_ProductID

然后,我将以下代码添加到 Userform 中,只要产品名称文本框的值发生更改并且失去焦点,就会触发该代码。

Private Sub TextBox_Productname_Change()

    ' Clear the content of all Textboxes but the Productname
    TextBox_ProductID.Value = ""

    ' Analyse the Cells in the Worksheet called "Products"
    With Worksheets("Products")

        ' Assume in the first row are the headers so start
        ' looking at the second row and go to the final one
        For i = 2 To .UsedRange.Rows.Count

            ' check whether the value in the first column
            ' matches the value of the Textbox_Productname
            If .Cells(i, 1) = TextBox_Productname.Value Then

                ' If there is a match copy the values of all
                ' the other columns to the textboxes
                TextBox_ProductID.Value = .Cells(i, 2)
            End If

        Next i

    End With

End Sub
于 2013-01-27T00:15:58.563 回答