通过下拉列表将数据从一张纸导出到另一张 – VBA Excelmacros

我有一个有两个工作表的Excel工作簿。 工作表1是一个填写的表单,它有一个提交button(用VBA创build),该button将数据提交给工作表2中的下一个空行。因此工作表2填充了以前提交的表单信息,工作表1可以清除(再次通过用VBA创build的button)准备好下一批信息。

每个条目都有一个唯一的编号供参考,但是我想要做的是在表1(表格)中列出所有可以select的唯一编号的下拉列表,并将所有相关信息返回到表单,所以可以进行任何编辑,一个button可以保存/覆盖数据,而不是将其另存为新行。

所以希望能够将数据带回到工作表1来编辑/修改/保存/覆盖。

我的VBA知识是有限的,因为这是我处理的第一个项目,所以我仍然在学习基本知识。

任何意见或build议将不胜感激。

非常感谢

瑞秋。

我已经放了一个快速示例演示你所要求的,它可以在这里下载。

我做了以下几点:

  1. 添加表单(带有一些数据编辑字段)和数据(带有示例数据)工作表。
  2. 在数据表中添加了validation下拉列表,显示了您的数据工作表中的ID +名称数据。
  3. 当validation下拉列表中选中的选项被更改时,将触发Worksheet_Change事件,运行以下代码:

    Private Sub Worksheet_Change(ByVal Target As Range) 'check that the Target cell is our dropdown If Target.Address = "$C$2" Then 'get the value of the dropdown using the range method Dim dropDownValue As String dropDownValue = CStr(wsForm.Range(Target.Address).Value) 'if that dropdown value exists (has a length of more than zero) If Len(dropDownValue) > 0 Then 'get the corresponding record from the data sheet Dim index As Integer index = Left(dropDownValue, 1) wsForm.Range("C3").Value = index wsForm.Range("C4").Value = Application.WorksheetFunction.VLookup(index, wsData.Range("A:E"), 2, False) wsForm.Range("C5").Value = Application.WorksheetFunction.VLookup(index, wsData.Range("A:E"), 3, False) wsForm.Range("C6").Value = Application.WorksheetFunction.VLookup(index, wsData.Range("A:E"), 4, False) End If End If End Sub 

    它使用vlookups从数据表中检索信息来填充编辑表单。

  4. 单击保存button时,将运行以下代码:

     Sub Button4_Click() Dim index As Integer index = wsForm.Range("C3") If index > 0 Then Dim foundIndexRange As Range Set foundIndexRange = wsData.Range("A:A").Find(index) If (Len(foundIndexRange.Value) > 0) Then foundIndexRange.Offset(0, 1).Value = wsForm.Range("C4").Value foundIndexRange.Offset(0, 2).Value = wsForm.Range("C5").Value foundIndexRange.Offset(0, 3).Value = wsForm.Range("C6").Value End If MsgBox "Record saved" Else MsgBox "Please choose from the dropdown" End If End Sub 

    其中使用了range.Find方法来定位我们的索引在数据表上的范围,然后用offset来覆盖我们的新值。

我希望这是有道理的,请问你是否有任何问题。

您可以打开“filter”并在唯一编号列中过滤/search您要更改其数据的编号,而不是将数据带回到表1中。 然后它将只显示对应于该号码的数据的input。 然后在表格2上进行编辑。

希望这是有用的。