【发布时间】:2017-08-05 00:29:57
【问题描述】:
我正在尝试将许多电子表格中的数据复制到一张工作表中。我正在从 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
【问题讨论】:
-
CopyRng的地址是什么? -
任一工作表,特别是在被复制(或目标)的范围内是否包含合并单元格?
-
只是好奇 - 您使用
With DestSh.Cells(Last + 1, 2)时是否遇到错误?另外,Last是Long还是Integer? -
尝试将您的复制范围设置为
Set CopyRng = Intersect(sh.UsedRange, sh.Range(sh.Rows(StartRow), sh.Rows(shLast)))。 -
另外,你的两个函数
LastRow和FindLastRow有什么区别——这可能是个问题,但你还没有向我们展示这些实现......