VBA에서 LOOPS를 사용하여 코드를 단순화하는 방법

VBA에서 LOOPS를 사용하여 코드를 단순화하는 방법

한 워크시트의 열에서 내용을 복사하여 다른 워크시트에 붙여넣어야 합니다(동시에 각 변수에 대해 총 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번 확인하고 매번 한 가지 작업을 수행하는 대신 한 번만 확인하고 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도 복사하면 안되나요?) 아마도 더 나은 방법이 있을 수 있지만 이는 간단한 루프의 예입니다.

각 열의 행 수가 다르기 때문에 이를 더 큰 루프로 처리하여 하루라고 부를 수는 없습니다.

몇 가지 작은 루프를 수행할 수 있지만 지금은 이를 사용자에게 맡깁니다. 행운을 빌어요.

편집하다

한 가지 의견은 동일한 작업을 여러 번 수행하지만 훨씬 적은 코드로 수행할 수 있는 한 가지 방법인 함수에 대해 이야기하는 것입니다.

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

이 첫 번째 서브는 두 번째 개인 서브를 호출합니다. 이것은 함수일 수도 있지만 무언가를 반환하려고 할 때 사용됩니다. 대신 우리는 개인 서브를 사용합니다:

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

두 번째 하위는 내 첫 번째 예일 뿐이지만 더 많은 변수가 추가되어 있습니다. 그런 다음 하위를 호출할 때 이러한 변수가 괄호 안에 설정됩니다.
데이터베이스의 C열에 고정된 숫자를 변수로 대체하여 계산이 더 쉬워졌습니다. 하지만 귀하의 필요에 더 적합하다면 전화할 때 고정 번호로 변경할 수 있습니다.

관련 정보