2017-05-15 15 views
0

の列のセクションをコピー&ペーストしようとしています。特定の条件を満たす行の最初の17列をコピーしようとしています。If文、実行時エラー1004

Sub CopyRowsAcross() 

'Name Worksheets 
    Dim e As Integer 
    Dim wsd2 As Worksheet: Set wsd2 = ThisWorkbook.Sheets("DataSheet2") 
    Dim wsBS As Worksheet: Set wsBS = ThisWorkbook.Sheets("Budget Summary") 


'Set variables 
For e = 3 To 1776 
Dim LastRow As Long 

LastRow = wsBS.UsedRange.Row - 1 + wsBS.UsedRange.Rows.Count 

'Set Criteria for copying lines across 
If ((IsEmpty(wsd2.Cells(e, 1).Value) = False And IsEmpty(wsd2.Cells(e, 4).Value)) = True) Or (IsEmpty(wsd2.Cells(e, 1).Value) = False And IsEmpty(wsd2.Cells(e, 4).Value) = False) Or (IsEmpty(wsd2.Cells(e, 1).Value) = True And IsEmpty(wsd2.Cells(e + 1, 5).Value) = False And IsEmpty(wsd2.Cells(e, 4).Value) = False) Then 

'Particulars of copying 
wsd2.Range(Cells(e, 1), Cells(e, 17)).Copy 
*wsBS.Range(Cells(LastRow + 1, 1)).PasteSpecial xlPasteValues* 
End If 


Next e 

End Sub 

スターを付けた行が問題として強調されていますが、コードは、もはやコードがある

「ワークシートが失敗したメソッド 『オブジェクトの範囲を』」と言って、ランタイムエラー1004をあきらめ、機能しません。

誰かが私にそれが動作していない理由とそれを修正する方法を教えてくれることを願っていますか? 多くのありがとう

答えて

0

指定しない場合、は現在のアクティブシートのセルを参照するので、必ずワークシートまたは範囲を指定してください。反対側に、細胞(rは、c)の範囲を返し、そうするペーストラインを簡略化することができる。

wsd2.Range(wsd2.Cells(e, 1), wsd2.Cells(e, 17)).Copy 
wsBS.Cells(LastRow + 1, 1).PasteSpecial xlPasteValues 
0

かわり反復のオートフィルタ機能を使用することができます。次に、フィルタリングされた範囲を必要なシートにコピーします。
あなたの場合は、次のようになります。

With wsd2 
     .AutoFilterMode = False 
     'Based on your example, you need 17 columns (hence Q) 
     .Range("A1:Q1").AutoFilter 
     'Set the criteria here - you can set multiple criterias if required 
     .Range("A1:Q1").AutoFilter Field:=<enter column number>, Criteria1:="<enter criteria for column>" 
     .Range("A1:Q1").Parent.AutoFilter.Range.Copy 
End With 

'Here I'm showing how to copy into a new sheet. 
Set WSNew = Worksheets.Add(After:=Sheets(ActiveSheet.Index)) 
WSNew.Name = "Budget Summary" 
'If sheet exists you can directly start from here and replace WSNew with wsBS 
With WSNew.Range("A1") 
    ' Paste:=8 will copy the columnwidth in Excel 2000 and higher 
    ' Remove this line if you use Excel 97 
    .PasteSpecial Paste:=8 
    .PasteSpecial xlPasteValues 
    .PasteSpecial xlPasteFormats 
    Application.CutCopyMode = False 
    .Select 
End With 
関連する問題