Excel-VBA: For-Schleife für die iteration durch Blatt-Palette

In dem folgenden Programm, was ich versuche zu tun ist, Scannen Sie das "ja" - Spalte einen bestimmten Bereich des sheets in einer Arbeitsmappe, die ein Benutzer ausfüllt, und wo immer der Benutzer legt ein "x" innerhalb dieser speziellen "ja" - Spalte, Reihe, es zu identifizieren wird das zugeordnete Element der Frage markiert diese Zeile und kopieren Sie die Artikel-code im Zusammenhang mit dieser Frage (z.B. C3), die in eine Zusammenfassung für logging-Zwecke.

Das problem ist, dass der code nicht kopieren Sie das Element auf der Seite Zusammenfassung, wie beabsichtigt, wenn die for-Schleife iteriert über die gewünschte Reichweite der Blätter. Wenn ich aber den Kommentar aus der for-Schleife code und schreiben Sheets(6).Select anstatt Sheets(i).Wählen Sie, zum Beispiel, wird es kopieren Sie die "x" markierten Elemente auf der Seite "Zusammenfassung" für Blatt index #6 wie beabsichtigt. Dies führt mich zu glauben, meine copy+paste Teil des Codes funktioniert (zwischen der while-loop-Anweisung), aber die for-Schleife scheitert irgendwie.

Kann jemand bitte mir helfen identifizieren Sie die Quelle des Fehlers? Ich verstehe, dass dieser code ist nicht effizient, wie die übermäßige Verwendung von .wählen Sie-und nicht-dynamische Erklärungen, aber wenn ich wollte zu halten, wie viel von dem code der gleiche ist wie möglich, wie könnte ich das ändern, so dass es eine Schleife durch alle Blätter, wie ich es wollte?

Dank

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
InformationsquelleAutor user2608147 | 2013-08-14
Schreibe einen Kommentar