2017-08-04 29 views
1

多くのスプレッドシートのデータを1つのシートにコピーしようとしています。私は範囲A:Lからコピーしており、サマリーシートのA:Lからデータを貼り付けると、問題なく動作します。列B:Mにデータを貼り付けようとしていますが、これは1004エラーが発生したときです。私はDomenicからWith DestSh.Cells(Last + 1, "A") Excelエラー1004:領域とペースト領域が同じサイズではありません

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

「CopyRng」の*アドレス*とは何ですか? –

+1

Doeはワークシートのいずれか、特にコピーされた(または宛先)の範囲にマージされたセルを含んでいますか? –

+0

ちょうど興味深い - あなたは 'With DestSh.Cells(Last + 1,2)'を使ってエラーを受け取りますか?また、 'Last'は' Long'か 'Integer'ですか? – BruceWayne

答えて

0

を変更したときに、私はエラーが表示さ

:私は、MSDNのサイトから少し変更されたコードを持って

としてあなたのコピー範囲を設定してみてください:

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))) 
関連する問題