vba 用于自动数据输入的 Excel 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9738938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:32:51  来源:igfitidea点击:

Excel macro for automatic data entry

excelvbaexcel-vba

提问by starcraftOnCrack

I need a macro to help me with data entry in Excel. Basically I want sheet 1for data entry, and sheet 2for the data that are entered. Sheet 1will only have one row for data entry, and once that row is filled you hit enter. The row is automatically added to the table in sheet 2, and the row is cleared on sheet 1. Now you are ready to enter another entry in sheet 2.

我需要一个宏来帮助我在 Excel 中输入数据。基本上我想要sheet 1数据输入,以及sheet 2输入的数据。Sheet 1将只有一行用于数据输入,一旦该行被填满,您就按回车键。该行将自动添加到 中的表中sheet 2,并且该行在 上被清除sheet 1。现在您已准备好在 中输入另一个条目sheet 2

So to summarize sheet 2will have multiple rows for the data entered, and sheet 1will only have 1 row because it automatically clears it's row after each entry.

所以总结sheet 2将有输入数据的多行,并且sheet 1只有 1 行,因为它会在每次输入后自动清除它的行。

Is something like this possible?? If you guys can post some code for me it would really help, and keep in mind I have never programmed in VBA before. Thanks in advance!!

这样的事情有可能吗??如果你们可以为我发布一些代码,那真的很有帮助,请记住,我以前从未用 VBA 编程过。提前致谢!!

回答by Jerry Beaucaire

Place a button on your sheet 1, then put this macro into a regular code module and attach it to your button. Assuming you are entering values across row2 (row1 being titles or such), then this would transfer row2 data to the next empty row on sheet2:

在您的工作表 1 上放置一个按钮,然后将此宏放入常规代码模块并将其附加到您的按钮上。假设您在第 2 行中输入值(第 1 行是标题等),那么这会将第 2 行数据传输到工作表 2 上的下一个空行:

Sub Transfer()
    Rows(2).Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
    Rows(2).ClearContents
End Sub

回答by tjg184

I'm not totally sure of the use-case, but have you considered using Excel UserForms instead.

我不完全确定用例,但您是否考虑过使用 Excel 用户表单。

The input would be a form that actually appears in front of the user which would collect input. When they hit the enter button, you could have underlying code to update the main sheet, Sheet 2.

输入将是一个实际出现在用户面前的表单,用于收集输入。当他们按下回车按钮时,您可以使用底层代码来更新主工作表 Sheet 2。

http://www.excel-vba-easy.com/vba-userform-excel-vba.html

http://www.excel-vba-easy.com/vba-userform-excel-vba.html

回答by SWa

You could also use the database form. set up your database sheet then Data > Form (in 2003)

您也可以使用数据库表单。设置您的数据库表,然后数据 > 表单(2003 年)

回答by Robert Ilbrink

In order not to have to "click" on a button, I would suggest to look into this function:

为了不必“单击”按钮,我建议查看此功能:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub

This macro will run every time something in your sheet has changed. It might be that you have to check if there is data in cell A1, as this VBA might be triggered both after hitting enter and after the data has been transported to the other sheet.

每次工作表中的某些内容发生更改时,此宏都会运行。可能是您必须检查单元格 A1 中是否有数据,因为在按 Enter 键和将数据传输到另一个工作表后都可能触发此 VBA。