[英]How to Copy Data from range to last row in table (transposed)?
Hello everyone I'm back for my second beginner VBA question (Same project as before)大家好,我回来回答我的第二个初学者 VBA 问题(与以前相同的项目)
Information: 2 Sheets - Database & Entry信息:2 张 - 数据库和条目
Both contain the same two tables just that Entry is transposed (headers in rows instead of columns)两者都包含相同的两个表,只是 Entry 被转置(行而不是列中的标题)
My goal is to copy the data from entry into the Databases' last row while adding an additional one.我的目标是将条目中的数据复制到数据库的最后一行,同时添加一个。 Ideally there would be a way to copy the formula's in data entry right over (or format the database columns to contain them)
理想情况下,有一种方法可以直接复制数据输入中的公式(或格式化数据库列以包含它们)
The data is sorted and filtered in the database afterwards, hence the need for a table.之后在数据库中对数据进行排序和过滤,因此需要一个表。 I'm having real trouble with this entire ListObject thing and understanding how it works.
我在整个 ListObject 的事情上遇到了真正的麻烦并理解它是如何工作的。
Returns run time error 1004: PasteSpecial method of range class failed on this line:在这一行返回运行时错误 1004:范围类的 PasteSpecial 方法失败:
tbl.Range(LastRow).PasteSpecial Paste:=xlValues, Transpose:=True
However if I change that line to:但是,如果我将该行更改为:
Sheets("Database").ListObjects("Entire").PasteSpecial Paste:=xlValues, Transpose:=True
i get a runtime 438 (object doesn't support this property or method) error, which i find very confusing cause they should be the same thing我收到运行时 438(对象不支持此属性或方法)错误,我觉得这很令人困惑,因为它们应该是同一件事
Thank you for any help or suggestions, they are greatly appreciated!感谢您的任何帮助或建议,非常感谢! Also slightly off-topic but if you know any good resource I could buy to improve feel free to let me know.
也有点偏离主题,但如果您知道我可以购买的任何好的资源来改进,请随时告诉我。
Private Sub CommandButton2_Click()
Dim Entry As Worksheet
Set Entry = ActiveSheet
Dim tbl As ListObject
Set tbl = Sheets("Database").ListObjects("Entire")
Dim LastRow As Integer
LastRow = tbl.Range.Rows.Count
Application.ScreenUpdating = False
If Entry.Range("E9") = "y" Then
Entry.Range("E6:E100").Copy
tbl.ListRows.Add AlwaysInsert:=True
tbl.Range(LastRow).PasteSpecial Paste:=xlValues, Transpose:=True
Entry.Columns("E").Delete
Else
Entry.Columns("E").Delete
End If
Application.ScreenUpdating = True
End Sub
It's very easy to clear the clipboard in Excel accidentally, so you should (probably) always call .Copy
immediately before .Paste/.PasteSpecial
.这很容易不小心清除Excel中的剪贴板,所以你应该(可能)随时拨打
.Copy
之前.Paste/.PasteSpecial
。 Changing the order of改变顺序
Entry.Range("E6:E100").Copy
tbl.ListRows.Add AlwaysInsert:=True
solves the problem.解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.