[英]Export data from one sheet to another via drop down list - VBA Excel Macro
I have an Excel workbook with two worksheets on. 我有一个带有两个工作表的Excel工作簿。 Sheet 1 is a form which gets filled in and it has a submit button (created with VBA) which takes the data and adds it to the next empty line on sheet 2. So sheet 2 is filled with previously submitted form information and sheet 1 can be cleared (again via a button created with VBA) ready for the next lot of information.
工作表1是填写后的表单,它具有一个提交按钮(使用VBA创建),该按钮将数据收集到表单2的下一个空行中。因此,工作表2填充有先前提交的表单信息,工作表1可以(再次通过使用VBA创建的按钮)被清除,以准备接下来的大量信息。
Each entry has a unique number for reference purposes, but what I would like to do is on sheet 1(the form) to have a drop down list of all the unique numbers which I can select one and for it to bring all the relevant information back in to the form so any edits can be made and a button to be able to save/overwrite the data instead of saving it as a new line. 每个条目都有一个唯一的编号以供参考,但是我想做的是在工作表1(表格)上列出所有唯一编号的下拉列表,我可以选择一个唯一编号并携带所有相关信息返回到窗体,以便可以进行任何编辑,还可以使用一个按钮来保存/覆盖数据,而不是将其另存为新行。
So would like to be able to bring the data back to sheet 1 to edit/amend/save/overwrite. 因此,我们希望能够将数据带回到工作表1进行编辑/修改/保存/覆盖。
My VBA knowledge is limited as this is the first project I have dealt with it on so I'm still learning the basics as I go. 我的VBA知识有限,因为这是我处理的第一个项目,因此我仍在学习基础知识。
Any advice or suggestions would be gratefully appreciated. 任何意见或建议,将不胜感激。
Many Thanks 非常感谢
Rachael. 雷切尔
Instead of bringing the data back to sheet 1, in sheet 2, you can turn on "Filters" and in the unique numbers column, filter/search for the number whose data you want to change. 您可以打开“过滤器”,然后在“唯一数字”列中过滤/搜索要更改其数据的数字,而不必将数据返回到工作表1的工作表2中。 It will then only show the entry of data corresponding to that number.
然后,它将仅显示与该编号相对应的数据条目。 Then make the edits on sheet 2.
然后在工作表2上进行编辑。
Hope this is useful. 希望这是有用的。
I've put together a quick example demoing what you requested, it can be downloaded here . 我整理了一个简短的示例来演示您的要求,可以在此处下载。
I did the following: 我做了以下工作:
When the selected option in the validation dropdown is changed it triggers the Worksheet_Change event, running the following code: 更改验证下拉列表中的选定选项后,它将触发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
Which uses vlookups to retrieve the information from the datasheet to populate the editing form. 它使用vlookups从数据表中检索信息以填充编辑表单。
When the save button is clicked, the following code is run: 单击保存按钮时,将运行以下代码:
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
Which uses the range.Find method to locate the range where our index is on the data sheet, then offset to overwrite our new values. 它使用range.Find方法定位索引在数据表上的范围,然后偏移以覆盖新值。
I hope that makes sense, please ask if you have any questions. 我希望这是有道理的,请问您是否有任何疑问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.