我正在尝试将许多电子表格中的数据复制到一张表格中。我从范围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
答案 0 :(得分:0)
来自多梅尼克:
尝试将复制范围设置为:
设置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)))