VBA - 将表格行复制并粘贴到另一个表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12299898/
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
VBA - Copy and Paste Table Row to Another Table
提问by user1651899
I am very new to VBA and I am trying to solve what I think should be a very simple problem (I have a Java/J2EE background)... I am looping through a table and want to copy rows to a table on another worksheet based on some conditional statements. See code below.
我对 VBA 很陌生,我正在尝试解决我认为应该是一个非常简单的问题(我有 Java/J2EE 背景)......我正在遍历一个表并想将行复制到另一个工作表上的表中基于一些条件语句。请参阅下面的代码。
Sub closeActionItems()
Dim i, iLastRow As Integer
Dim date1 As Date
Dim oLastRow As ListRow
date1 = Date
iLastRow = ActiveSheet.ListObjects("Open_Items").ListRows.Count
For i = 6 To iLastRow
If Cells(i, 7).Value <= date1 And Cells(i, 7).Value <> vbNullString Then
Rows(i).Copy
Set oLastRow = Worksheets("Closed").ListObject("Closed_Items").ListRows.Add
'Paste into new row
Application.CutCopyMode = False
Rows(i).EntireRow.Delete
End If
Next
End Sub
I have tried many different iterations but have been unable to find the correct way to copy the contents of the clipboard to the newly created row.
我尝试了许多不同的迭代,但一直无法找到将剪贴板的内容复制到新创建的行的正确方法。
Any help would be appreciated. Thanks ahead of time.
任何帮助,将不胜感激。提前致谢。
采纳答案by PaulStock
Define srcRow as a Range like so:
将 srcRow 定义为 Range,如下所示:
Dim srcRow as Range
Then in your loop try doing this:
然后在你的循环中尝试这样做:
Set srcRow = ActiveSheet.ListObjects("Open_Items").ListRows(i).Range
Set oLastRow = Worksheets("Closed").ListObjects("Closed_Items").ListRows.Add
srcRow.Copy
oLastRow.Range.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Rows(i).EntireRow.Delete
Notice that you still have a problem in that you are deleting rows as you are trying to loop through them, so you probably want to change your loop so that it starts at the bottom and goes up.
请注意,您仍然有一个问题,因为您在尝试遍历行时正在删除行,因此您可能希望更改循环,使其从底部开始向上移动。