Excel错误1004:复制区域和粘贴区域大小不一样

我试图从许多电子表格复制数据到一张纸上。 我从范围A:L复制,当数据从汇总表上的A:L粘贴时,它没有问题。 我试图粘贴我的数据列B:M,这是我收到1004错误。 我有一个从MSDN网站稍作修改的代码:

我将With DestSh.Cells(Last + 1, "A")更改为With DestSh.Cells(Last + 1, "B")时收到错误

  ' Loop through all worksheets and copy the data to the ' summary worksheet. For Each sh In ActiveWorkbook.Worksheets If sh.Name <> DestSh.Name And sh.Name <> "Pivot Table" And sh.Name <> "Combined" Then ' Find the last row with data on the summary ' and source worksheets. Last = LastRow(DestSh) shLast = FindLastRow(sh) ' If source worksheet is not empty and if the last ' row >= StartRow, copy the range. If shLast > 0 And shLast >= StartRow Then 'Set the range that you want to copy Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast)) StartRow = 2 ' Test to see whether there are enough rows in the summary ' worksheet to copy all the data. If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then MsgBox "There are not enough rows in the " & _ "summary worksheet to place the data." GoTo ExitTheSub End If ' This statement copies values and formats. CopyRng.Copy With DestSh.Cells(Last + 1, "A") .PasteSpecial xlPasteValues Application.CutCopyMode = False End With End If End If Next 

来自Domenic:

尝试设置您的复制范围为:

Set CopyRng = Intersect(sh.UsedRange,sh.Range(sh.Rows(StartRow),sh.Rows(shLast)))

解:

  If shLast > 0 And shLast >= StartRow Then 'Set the range that you want to copy 'Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast)) 'StartRow = 2 Set CopyRng = Intersect(sh.UsedRange, sh.Range(sh.Rows(StartRow), sh.Rows(shLast)))