So fügen Sie eine Schleife mit einem Zähler in vba

Ich habe eine Spalte mit IDs in eine Excel-Tabelle namens Tabelle1. Ich habe Daten, das entspricht der IDs in den Spalten rechts von der Spalte a Die Anzahl von Zellen in einer Zeile variiert. Zum Beispiel:

A, B, C, D, E, F, ...

John, 5, 10, 15, 20

Jacob, 2, 3

Jingleheimmer, 5, 10, 11

Ich versuche zu kopieren, die Daten in ein neues Arbeitsblatt, Tabelle5, in dem folgenden format:

A, B, C, D, E, F, ...

John, 5

John, 10

John, 15

John, 20

Jacob, 2

Jacob, 3

Jingleheimmer, 5

Jingleheimmer, 10

Jingleheimmer, 11

Schrieb ich folgenden code, der Kopien in den ersten zwei IDs. Ich könnte weiter kopieren und einfügen der zweiten Hälfte des Codes und ändern Sie einfach die Zellen, allerdings habe ich 100te IDs. Dies würde zu lange dauern. Ich denke, wenn ein Vorgang wird wiederholt, ich sollte mit einer Schleife. Können Sie mir helfen, verwandeln diese sich wiederholenden code in eine Schleife?

Sub Macro5()

Dim LastRowA As Integer
Dim LastRowB As Integer

''' Process of copying over first ID '''

'grab all data cells in B2 to the right
With Sheets("Sheet1").Select
Range("B2", Range("B2").End(xlToRight)).Select
Selection.Copy
End With

'paste that data into the first empty cell of Column B in Sheet5
With Sheets("Sheet5").Select
Range("B1").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
End With

'grab the corresponding ID in cell A2
With Sheets("Sheet1").Select
Range("A2").Select
Application.CutCopyMode = False
Selection.Copy
End With

'paste the corresponding ID into the first empty cell of Column A in Sheet5
With Sheets("Sheet5").Select
LastRowB = Cells(Rows.Count, "B").End(xlUp).Row
Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("A1:A" & LastRowB)
End With

''' Repeat that process for each row in Sheet1 '''

'grab all data cells in B3 to the right
With Sheets("Sheet1").Select
Range("B3", Range("B3").End(xlToRight)).Select
Selection.Copy
End With

'paste that data into the first empty cell of Column B in Sheet5
With Sheets("Sheet5").Select
LastRowB = Cells(Rows.Count, "B").End(xlUp).Row
Range("B" & LastRowB + 1).Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
End With

'grab the corresponding ID in cell A3
With Sheets("Sheet1").Select
Range("A3").Select
Application.CutCopyMode = False
Selection.Copy
End With

'paste the corresponding ID into the first empty cell of column A in Sheet5
'and autofill down to the last populated cell in column B
With Sheets("Sheet5").Select
LastRowA = Cells(Rows.Count, "A").End(xlUp).Row + 1
Range("A" & LastRowB + 1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Application.CutCopyMode = False
LastRowB = Cells(Rows.Count, "B").End(xlUp).Row
Selection.AutoFill Destination:=Range("A" & LastRowA & ":A" & LastRowB)
End With

End Sub
InformationsquelleAutor tulanejosh | 2016-04-07
Schreibe einen Kommentar