Excel VBA复制/粘贴范围到另一个工作表中的下一个可用单元格

时间:2015-07-14 14:30:52

标签: excel vba copy range paste

我想要做什么:过滤数组,然后将过滤后的数据复制到另一个工作簿上。然后将刚刚粘贴到同一工作簿中的另一个工作表上的数据复制到现有数据中。

我的想法:我在下面使用的代码用于帮助我从一个工作簿复制并粘贴到另一个工作簿并且工作得很好,

wb.Sheets("2014 Current Week").Range("C2:CC10000").Copy nwb.Sheets("2014 YTD").Range("C" & Rows.Count).End(xlUp).Offset(1, 0) 

但是如果我想复制到同一个工作簿中,它似乎不会以相同的方式工作。任何帮助,将不胜感激。谢谢!

Dim wb as workbook
Dim strs As String
Dim str As String
Dim nwb as workbook


Set wb = ThisWorkbook

strs = wb.Sheets("Macros").Range("H5") 'the 2014 address can be found in full in cell H5 in the Macros tab

set nwb = Workbooks.Open(strs) 'address of new workbook and opens it

With ActiveSheet

.AutofilterMode = False
'Filter this and that here'

End With



 nwb.Sheets("ALL DATA").Range("A1:CA100000").Copy wb.Sheets("2014 Current Week").Range("C" & Rows.Count).End(xlUp) 
'this one works, and copies exactly as I want into the 2014 Current Week tab

 wb.Sheets("2014 Current Week").Range("C2:CC10000").Copy wb.Sheets("2014 YTD").Range("C" & Rows.Count).End(xlUp).Offset(1, 0) 
'this one doesn't work, and does not copy or paste at all from that 2014 Current Week into the 2014 YTD tab of the same workbook

2 个答案:

答案 0 :(得分:1)

您的代码看起来不错,不应该有任何问题。由于它似乎不起作用,您需要查看实际的Worksheets和所涉及的数据。

您正在使用End查找最后一行数据。鉴于此,值得自己测试一下。转到Worksheet列中C的最后一行,然后点击 CTRL + UP 。这将显示要粘贴数据的位置。

根据您的描述,最后一行是问题所在。由于存在一些影响End的迷路数据。

答案 1 :(得分:0)

我刚刚测试了你的代码,它运行正常。它正确粘贴数据。检查表格的名称。

相关问题