简体   繁体   English

通过下拉列表将数据从一张纸导出到另一张纸-VBA Excel Macro

[英]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: 我做了以下工作:

  1. Added Form (with a few data editing fields) and Data (with example data) worksheets. 添加了表单(带有一些数据编辑字段)和数据(带有示例数据)工作表。
  2. Added a validation dropdown to the datasheet showing the ID + Name data from your Data worksheet. 在数据表中添加了一个验证下拉列表,其中显示了数据工作表中的ID +名称数据。
  3. 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从数据表中检索信息以填充编辑表单。

  4. 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.

相关问题 Excel 或 VBA - 从另一个工作表中的动态列填充下拉列表,没有重复项 - Excel or VBA - populate a drop down list from a dynamic column in another sheet with no duplicates Excel 将内容从一张纸复制到另一张纸,无需 VBA/宏 - Excel copy contents from one sheet to another without VBA/Macro Excel-将单元格+宏+ VBA从一张纸复制到另一张纸? - Excel - copy cells+macro+VBA from one sheet to another? VBA 代码为 select 过滤器值从一张表的下拉菜单中,并在另一张工作表的表格中应用过滤器 - Excel 2010 - VBA code to select filter value from drop down menu on one sheet and apply filter in a table on another worksheet - Excel 2010 根据下拉列表选择索引另一张纸的数据 - Indexing data from another sheet based on drop down list selection VBA 宏将数据从一个 excel 文件复制到另一个 - VBA macro to copy data from one excel file to another 如何使用VBA在Excel中读取工作表上的下拉列表的值 - How to read the value of a drop down list on a sheet in Excel using VBA 将数据从一张纸追加到另一个Excel VBA - Appending data from one sheet to another Excel VBA 从Word调用VBA excel宏以粘贴基于Word中的下拉列表进行更新的表 - Call VBA excel macro from word to paste a table that updates based on drop-down list in word Excel VBA代码可将数据从一张纸复制到另一张纸 - Excel VBA code that copies data from one sheet into another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM