VBA でループを使用してコードを簡素化するにはどうすればよいですか

VBA でループを使用してコードを簡素化するにはどうすればよいですか

あるワークシートの列の内容をコピーして別のワークシートに貼り付ける必要があります (同時に各変数を合計 7 コピーします)。複数の列があり、各列の変数の数は異なります。残念ながら、データのサイズが原因で、現在のコードは実行できません。ループを使用して記述できると思います。

私の考えでは、Excel が列内のテキストを検出できる場合、その下のセルをコピーして範囲内に貼り付ける (つまり 7 つのコピーを作成する) 必要があります。これが理にかなっていることを願います。どなたか助けていただければ幸いです。

これが私のコードです:

Sub SimpleCopy()

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E3").copy Worksheets("Data Base").range("C114:C120")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E4").copy Worksheets("Data Base").range("C121:C127")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E5").copy Worksheets("Data Base").range("C128:C134")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E6").copy Worksheets("Data Base").range("C135:C141")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E7").copy Worksheets("Data Base").range("C142:C148")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E8").copy Worksheets("Data Base").range("C149:C155")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E9").copy Worksheets("Data Base").range("C156:C162")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E10").copy Worksheets("Data Base").range("C163:C169")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E11").copy Worksheets("Data Base").range("C170:C176")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E12").copy Worksheets("Data Base").range("C177:C183")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E13").copy Worksheets("Data Base").range("C184:C190")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E14").copy Worksheets("Data Base").range("C191:C197")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2")) = True Then
Worksheets("Investor Data").range("E15").copy Worksheets("Data Base").range("C198:C204")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("F2")) = True Then
Worksheets("Investor Data").range("F2").copy Worksheets("Data Base").range("B205")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("F2")) = True Then
Worksheets("Investor Data").range("F3").copy Worksheets("Data Base").range("C205:C211")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("F2")) = True Then
Worksheets("Investor Data").range("F4").copy Worksheets("Data Base").range("C212:C218")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("F2")) = True Then
Worksheets("Investor Data").range("F5").copy Worksheets("Data Base").range("C219:C225")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("G2")) = True Then
Worksheets("Investor Data").range("G2").copy Worksheets("Data Base").range("B226")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("G2")) = True Then
Worksheets("Investor Data").range("G3").copy Worksheets("Data Base").range("C226:C232")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("G2")) = True Then
Worksheets("Investor Data").range("G4").copy Worksheets("Data Base").range("C233:C239")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("G2")) = True Then
Worksheets("Investor Data").range("G5").copy Worksheets("Data Base").range("C240:C246")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("G2")) = True Then
Worksheets("Investor Data").range("G6").copy Worksheets("Data Base").range("C247:C253")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H2").copy Worksheets("Data Base").range("B254")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H3").copy Worksheets("Data Base").range("C254:C260")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H4").copy Worksheets("Data Base").range("C261:C267")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H5").copy Worksheets("Data Base").range("C268:C274")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H6").copy Worksheets("Data Base").range("C275:C281")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H7").copy Worksheets("Data Base").range("C282:C288")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H8").copy Worksheets("Data Base").range("C289:C295")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H9").copy Worksheets("Data Base").range("C296:C302")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H10").copy Worksheets("Data Base").range("C303:C309")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H11").copy Worksheets("Data Base").range("C310:C316")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H12").copy Worksheets("Data Base").range("C317:C323")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("H2")) = True Then
Worksheets("Investor Data").range("H13").copy Worksheets("Data Base").range("C324:C330")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("I2")) = True Then
Worksheets("Investor Data").range("I2").copy Worksheets("Data Base").range("B331")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("I2")) = True Then
Worksheets("Investor Data").range("I3").copy Worksheets("Data Base").range("C331:C337")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("I2")) = True Then
Worksheets("Investor Data").range("I4").copy Worksheets("Data Base").range("C338:C344")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("I2")) = True Then
Worksheets("Investor Data").range("I5").copy Worksheets("Data Base").range("C345:C351")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("J2")) = True Then
Worksheets("Investor Data").range("J2").copy Worksheets("Data Base").range("B352")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("J2")) = True Then
Worksheets("Investor Data").range("J3").copy Worksheets("Data Base").range("C352:C358")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("J2")) = True Then
Worksheets("Investor Data").range("J4").copy Worksheets("Data Base").range("C359:C365")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("J2")) = True Then
Worksheets("Investor Data").range("J5").copy Worksheets("Data Base").range("C366:C372")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("J2")) = True Then
Worksheets("Investor Data").range("J6").copy Worksheets("Data Base").range("C373:C379")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("J2")) = True Then
Worksheets("Investor Data").range("J7").copy Worksheets("Data Base").range("C380:C386")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("J2")) = True Then
Worksheets("Investor Data").range("J8").copy Worksheets("Data Base").range("C387:C393")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K2").copy Worksheets("Data Base").range("B394")
End If

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K3").copy Worksheets("Data Base").range("C394:C400")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K4").copy Worksheets("Data Base").range("C401:C407")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K5").copy Worksheets("Data Base").range("C408:C414")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K6").copy Worksheets("Data Base").range("C415:C421")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K7").copy Worksheets("Data Base").range("C422:C428")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K8").copy Worksheets("Data Base").range("C429:C435")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K9").copy Worksheets("Data Base").range("C436:C442")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K10").copy Worksheets("Data Base").range("C443:C449")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("K11").copy Worksheets("Data Base").range("C450:C456")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("L2")) = True Then
Worksheets("Investor Data").range("L2").copy Worksheets("Data Base").range("B457")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("L3").copy Worksheets("Data Base").range("C457:C463")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("L4").copy Worksheets("Data Base").range("C464:C470")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("L5").copy Worksheets("Data Base").range("C471:C477")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("L6").copy Worksheets("Data Base").range("C478:C484")
End If


If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("K2")) = True Then
Worksheets("Investor Data").range("L7").copy Worksheets("Data Base").range("C485:C491")
End If

End Sub

答え1

まず、ようこそ。
次に、簡単に回避できる最初のことは、同じことをif何度も繰り返すことです。

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").range("E2"))したがって、13 回チェックしてそのたびに 1 つのことを実行する代わりに、1 回チェックして 13 のことすべてを実行できます。

それから、もちろん、あなたが尋ねているループの部分になります。
さまざまな方法でループする方法については多くの場所で読むことができますが、パターンがあればループを行うことができます。

したがって、最初の部分ではE2、次のようなループを作成できます。

If Application.WorksheetFunction.IsText(Worksheets("Investor Data").Range("E2")) = True Then
j = 114
For i = 3 To 15
    Worksheets("Investor Data").Range("E" & i).copy Worksheets("Data Base").Range("C" & j & ":C" & j + 6)
    j = j + 7
Next i
End If

for loopこれは、行番号を使用される変数に置き換えることにより、によって指定された E3 から E15 まで実行されます。(余談ですが、E2もコピーした方が良いのではないでしょうか?) おそらくこれよりももっと良い方法があるでしょうが、これは単純なループの例です。

各列の行数が異なるため、これを単に大きなループに挿入して終了することはできません。

もっと小さなループをいくつか作ることもできますが、今のところはあなたにお任せします。頑張ってください。

編集

あるコメントでは関数について言及されていますが、これは同じことを何度も実行する方法の 1 つですが、コードははるかに少なくなります。

Sub callCopy()

Dim startRow As Long
startRow = 114
Call copySub(startRow, "E", 15)
Call copySub(startRow, "F", 5)
Call copySub(startRow, "G", 6)
Call copySub(startRow, "H", 13)
Call copySub(startRow, "I", 5)
Call copySub(startRow, "J", 8)
Call copySub(startRow, "K", 11)
Call copySub(startRow, "L", 7)

End Sub

この最初のサブは 2 番目のプライベート サブを呼び出します。これは関数でもかまいませんが、何かを返したいときに使用します。代わりに、プライベート サブを使用します。

Private Sub copySub(startRow As Long, iCol As String, iRows As Long)
Dim i As Long
If Application.WorksheetFunction.IsText(Worksheets("Investor Data").Range(iCol & "2")) = True Then
For i = 2 To iRows
    Worksheets("Investor Data").Range(iCol & i).copy Worksheets("Data Base").Range("C" & startRow & ":C" & startRow + 6)
    startRow = startRow + 7
Next i
End If

End Sub

2 番目のサブは最初の例ですが、より多くの変数が入っています。これらは、サブを呼び出すときに括弧内に設定されます。
データベースの列 C の固定数を変数に置き換えたので、計算が簡単になります。ただし、必要に応じて、呼び出し時に固定数に変更することもできます。

関連情報