gpt4 book ai didi

vba - Excel 宏 - 将数据复制到新行

转载 作者:行者123 更新时间:2023-12-04 21:34:41 26 4
gpt4 key购买 nike

我以前从未使用过excel来做这样的事情,所以可以引用一些建议。

我有一个非常简单的表单,它有一个基本表单,当用户完成表单时,我希望他们单击​​保存按钮,然后将表单中的数据插入到新行中。

enter image description here

希望这张图片能解释。 Marcus 的详细信息已添加到表单中,当单击 SAVE 时,我需要添加一个包含 Marcus 详细信息的新行 (11)。

这可能吗 ?有人能指出我正确的方向吗?

这是我第一次在 excel 中查看宏并做类似的事情。

使用宏记录器,我可以从 C3:C5 和 G3:G5 复制数据并将它们粘贴到第 11 行,但是如何添加新行并粘贴到该行。最后,如何将宏绑定(bind)到 SAVE 单元格?

Sub Copy()
'
' Copy Macro
'

'
Range("C3:C5").Select
Selection.Copy
Range("A11").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("G3:G5").Select
Application.CutCopyMode = False
Selection.Copy
Range("D11").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End Sub

最佳答案

左下角的宏记录器是一个很好的起点(它是上面覆盖着红色圆圈的表格):

enter image description here

如果它不存在,则右键单击并选择它(如下所示):

enter image description here

然后记录你想要自动发生的事情,这就是你的起点

更新:

您需要创建一个保存按钮,在这里您可以创建一个按钮并将复制宏分配给:

enter image description here

这是您更新的代码(请参阅我在“'”符号之后包含的注释:

Sub Copy()

Range("C3:C5").Copy ' this replaces the select, then copy steps and is better syntax
Range("A" & Range("A" & Cells.Rows.Count).End(xlUp).Row + 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True ' The pastes relative to the last row (you code was an absolete referance to row 11 - hence it being overwritten)
Range("G3:G5").Copy ' As per first comment
Range("D" & Range("D" & Cells.Rows.Count).End(xlUp).Row + 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True ' As per second comment
Application.CutCopyMode = False ' escapes from copy/paste mode

End Sub

关于vba - Excel 宏 - 将数据复制到新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42085308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com