ExcelワークシートにSheet1というID列があります。列Aの右側の列にあるIDに対応するデータがあります。行内のセルの量はさまざまです。例えば:vbaにカウンタを含むループを追加する方法
A、B、C、D、E、F、...
ジョン、5、10、15、20
ヤコブ、2、3
Jingleheimmer、 5、10、11
私がコピーしようとしているその次の形式で新しいワークシート、のSheet5、へのデータ:
A、B、C、D、E、F、...
ジョン、5
ジョン、10
ジョン、15
ジョン、20
ヤコブ、2
ヤコブ、3
Jingleheimmer、5
Jingleheimmer、10
Jingleheimmer、11
私は最初の2つのIDの上にコピーし、次のコードを書きました。コードの後半部分を貼り付けてコピーして、セルを変更することはできますが、IDは100個あります。これには時間がかかります。私はプロセスが繰り返されるたびにループを使用するべきだと思います。この繰り返しコードをループに変えるのを助けてくれますか?
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
ワウ!それは本当にうまくいったし、シンプルすぎる。ありがとうございました。接線的な質問:基本的なカウントの最後のローからコピーしてネストされたループに貼り付けるにはどうすればよいですか?私はあなたのコードを理解することができますが、助けなしにそのようなものを作成することは難しいです。 – tulanejosh