2013-08-14 5 views
0

다음 프로그램에서 사용자가 채우는 통합 문서의 특정 범위 시트의 "예"열을 스캔하고 무엇을 하려는지, 그리고 사용자 해당 "예"열 범위에 "x"를 입력하면 해당 행에 표시된 질문의 관련 항목을 식별하고 해당 질문과 관련된 항목 코드 (예 : C3)를 기록 목적의 요약 페이지로 복사합니다.Excel VBA : 반복을위한 시트 범위를 통해 반복

문제는 for 루프가 원하는 시트 범위를 반복 할 때 코드가 의도 한대로 요약 페이지에 항목을 복사하지 않는다는 것입니다. 그러나 for 루프 코드를 주석으로 처리하고 Sheets (i) .Select 대신 .Select (6)를 쓰면 예를 들어 "x"표시된 항목이 시트 색인 # 6의 요약 페이지에 복사됩니다 . 이것은 내가 코드 + 복사 붙여 넣기 부분을 (while 루프 문 사이에) 작동하지만, for 루프가 어떻게 든 실패한다고 생각하게한다.

누군가가 오류의 원인을 파악하는 데 도움을 줄 수 있습니까? 나는이 코드가 .select와 non-dynamic 선언을 과도하게 사용하는 것과 같이 효율적이지 않다고 생각하지만 가능한 한 많은 코드를 유지하고 싶다면 어떻게 수정하여 모든 코드를 반복 할 수 있을까? 내가 의도 한대로 시트?

감사

Sub DSR_Autofill() 

' Variable Declarations: 

Dim x_count As Long  'keeps track of how many "x"s you have 
Dim i As Long   'for loop index 
Dim n As Long   'while loop index 
Dim item_a As String 'Letter part of Item 
Dim item_b As String 'Number part of Item 

' Variable Initializations: 

x_count = 0    'start x count at zero 

' Clear Previous Data: 

Sheets(2).Range("A25:A29").ClearContents  'Clear Summary Pages before scanning through 
Sheets(3).Range("A18:A200").ClearContents 

' Main Data Transfer Code: 

For i = 5 To i = 20   'Starts at "Process Control" and ends on "Product Stewardship" 

    Sheets(i).Select  'Select current indexed worksheet and... 
    Range("D15").Select  '...the first item cell in the "Yes" Column 
    n = 0     'initialize n to start at top item row every time 

     Do While ActiveCell.Offset(n, -3) <> Empty  'Scan down "YES" column until Item Column (just "A" Column)... 
                 '...has no characters in it (this includes space (" ")) 
      If (ActiveCell.Offset(n, 0) = "x" _ 
      Or ActiveCell.Offset(n, 0) = "X") Then  'If an "x" or "X" is marked in the "YES" column at descending... 
                 '...cells down the column, at an offset specified by the for loop index n 

       item_a = ActiveCell.Offset(n, -3).Value  ' Store Letter value 
       item_a = Replace(item_a, "(", "")   ' Get rid of "(", ")", and " " (space) 
       item_a = Replace(item_a, ")", "")   ' characters that are grabbed 
       item_a = Replace(item_a, " ", "") 

       item_b = ActiveCell.Offset(n, -2).Value  ' Store number value 
       item_b = Replace(item_b, "(", "")   ' Get rid of "(", ")", and " " (space) 
       item_b = Replace(item_b, ")", "")   ' characters that are grabbed 
       item_b = Replace(item_b, " ", "") 

       x_count = x_count + 1      ' increment the total x count 

        If (x_count > 5) Then     ' If there are more than 5 "x" marks... 

         Sheets("SUMMARY P.2").Activate  ' ...then continue to log in SUMMARY P.2 and... 
         Range("A18").Select     ' ...choose "Item" column, first cell 
         ActiveCell.Offset((x_count - 6), 0).Value = (item_a & item_b) 

         'Insert concatenated value of item_a and item_b (for example "A" & "1" = "A1") 
         'at the cells under the "Item" column, indexed by x_count 

        Else         ' If there are less than 5 "x" marks... 

         Sheets("SUMMARY P.1").Activate  ' ...log in SUMMARY P.1 and... 
         Range("A25").Select     ' ...choose "Item" column, first cell 
         ActiveCell.Offset((x_count - 1), 0).Value = (item_a & item_b) 

        End If 

      End If 

      n = n + 1 
      Sheets(i).Select  'Return back to current sheet before running again 
      Range("D15").Select 

     Loop   'syntax for continuation of while loop 

Next i   'syntax for continuation of for loop 

If (x_count > 5) Then    'Bring user back to the Summary Page where the last Item was logged 

    Sheets("SUMMARY P.2").Select 

Else 

    Sheets("SUMMARY P.1").Select 

End If 

End Sub 

답변

1

"내가 ="에서 두 번째를 꺼내 당신의 라인 :

For i = 5 To 20 
+0

와우 O_O ... 하하 ... 감사합니다! 여러분이 저에게 준 다른 코드는 멋졌지만 많은 사람들이이 프로그램을 사용하기 때문에 원본을 수정할 수 없었습니다. – user2608147

+0

걱정하지 마세요. 기꺼이 도와 드리겠습니다. :) – tigeravatar

관련 문제