簡體   English   中英

Excel錯誤1004:復制區域和粘貼區域的大小不同

[英]Excel error 1004: Copy area and paste area aren't the same size

我正在嘗試將許多電子表格中的數據復制到一張紙上。 我正在從范圍A:L復制,並且當從摘要表中的A:L粘貼數據時,它沒有問題。 我正在嘗試將數據粘貼到B:M列上,這是當我收到1004錯誤時。 我在MSDN站點上有一個稍微修改的代碼:

當我將With DestSh.Cells(Last + 1, "A")更改為With DestSh.Cells(Last + 1, "B")時,我收到錯誤With DestSh.Cells(Last + 1, "A")

 ' 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:

嘗試將復制范圍設置為:

設置CopyRng =相交(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)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM