2017-08-22 8 views
0

複数の/ネストされたループの作成に問題があります。私はコードのブロックを書くことができ、毎回わずかな変更で17回繰り返すことができましたが、私はこれを避けようとしています。Excel VBAの複数のループ

私のコードは次のように見えますが、他のものはすべて、ループの必要がある最後の部分を除いては問題ありません。

したがって、ループには、基本的に "AJ2、AK2、...、AZ2"と "1,2、...、17"が含まれている必要があります。

ご協力いただければ幸いです。私は数週間これと闘ってきました。

Sub Test() 

Worksheets("SheetName").Activate 
Range("A1").CurrentRegion.Copy 
Worksheets("NewSheet").Activate 

Range("A1").PasteSpecial 
Range("A1").PasteSpecial xlPasteColumnWidths 

Dim c As Range 
Dim col1 As Range 
Dim j As Integer 
Dim Source As Worksheet 
Dim Target As Worksheet 
Dim lastrow As Long 

Set Source = ActiveWorkbook.Worksheets("SheetName") 
Set Target = ActiveWorkbook.Worksheets("NewSheet") 
Set col1 = Source.Range("SheetName[Col]") 

j = 2  

For Each c In col1 
    If c >= Range("AJ2") Then 'loop needed with "AJ2" 
     Source.Range("A" & c.Row & ":BN" & c.Row).Copy 
     Target.Range("A" & Range("E65536").End(xlUp).Row + 1).PasteSpecial xlPasteValues 
     Target.Range("BO" & Rows.Count).End(xlUp).Value = DateAdd("m", 1, Range("BO2").Value) 'loop needed with 1 
     j = Range("A65536").End(xlUp).Row + 1 
    End If 
Next c 

For Each c In col1 
    If c >= Range("AK2") Then 'loop needed with "AK2" 
     Source.Range("A" & c.Row & ":BN" & c.Row).Copy 
     Target.Range("A" & Range("E65536").End(xlUp).Row + 1).PasteSpecial xlPasteValues 
     Target.Range("BO" & Rows.Count).End(xlUp).Value = DateAdd("m", 2, Range("BO2").Value) 'loop needed with 2 
     j = Range("A65536").End(xlUp).Row + 1 
    End If 
Next c 

End Sub 
+0

Range( "AJ2") '&cをとる*関数*を記述してください。 *パラメータとして* – Bathsheba

答えて

0

範囲を名前で参照する代わりに、cells([rownumber], [columnnumber])を使用してください。こうすることで、テキストではなく数字が増えるループを簡単に作成できます。

関連する問題