引用:
A列のコードをB列の回数によってD列の結果に代入されていきます。
(標準モジュール)
----------------------------------------------------------------------
Sub Test1()
Dim wsSource As Worksheet
Dim lngSourceColumn As Long
Dim lngSourceFirstRow As Long
Dim lngSourceLastRow As Long
'複写元シートの参照。ここではアクティブシートとする
Set wsSource = ActiveSheet
With wsSource
'複写元となる列の番号
lngSourceColumn = 1
'その列の最初のデータ行の番号
lngSourceFirstRow = 2
'その列の最後のデータ行の番号を取得
lngSourceLastRow = .Cells(.Rows.Count, lngSourceColumn).End(xlUp).Row
'データ行がなければ終了
If lngSourceLastRow < lngSourceFirstRow Then
Set wsSource = Nothing
Exit Sub
End If
End With
Dim wsDestination As Worksheet
Dim lngDestinationColumn As Long
Dim lngDestinationFirstRow As Long
Dim lngDestinationLastRow As Long
'複写先シートの参照。ここでは複写元シートと同じシートにする
Set wsDestination = wsSource
With wsDestination
'複写先となる列の番号
lngDestinationColumn = 4
'その列の最初のデータ行の番号
lngDestinationFirstRow = 2
'その列の最後のデータ行の番号を取得
lngDestinationLastRow = .Cells(.Rows.Count, lngDestinationColumn).End(xlUp).Row
'既に1件以上のデータ行が複写先の列に存在する場合
If lngDestinationLastRow >= lngDestinationFirstRow Then
'それらのセル範囲の値/数式をクリアする
.Range(.Cells(lngDestinationFirstRow, lngDestinationColumn), _
.Cells(lngDestinationLastRow, lngDestinationColumn)).ClearComments
End If
End With
Dim rngSourceCell As Range
Dim lngSourceRow As Long
Dim rngDestinationCell As Range
Dim lngDestinationRow As Long
Dim lngCopyCount As Long
Dim lngRepeat As Long
'複写先となるセルの行番号の初期値を設定
lngDestinationRow = lngDestinationFirstRow
'複写元の最初の行から最後の行まで繰り返す
For lngSourceRow = lngSourceFirstRow To lngSourceLastRow
'複写元セルの参照
Set rngSourceCell = wsSource.Cells(lngSourceRow, lngSourceColumn)
'複写元セルの1つ右のセルに、数値データに変換できる値が格納されている場合
If IsNumeric(rngSourceCell.Offset(0, 1).Value) Then
'複写回数の取得
lngCopyCount = CLng(rngSourceCell.Offset(0, 1).Value)
'数値データに変換できない値が格納されている場合
Else
'複写回数を0にする
lngCopyCount = 0
End If
'複写回数の分だけループする(複写回数が0以下ならループしない)
For lngRepeat = 1 To lngCopyCount
'複写先セルの参照
Set rngDestinationCell = wsDestination.Cells(lngDestinationRow, lngDestinationColumn)
'複写先セルの表示形式を複写元セルと同じ形式にする
rngDestinationCell.NumberFormat = rngSourceCell.NumberFormat
'複写先セルに複写元セルの値を代入する
rngDestinationCell.Value = rngSourceCell.Value
Set rngDestinationCell = Nothing
'次の複写先は 1 つ下のセルにする
lngDestinationRow = lngDestinationRow + 1
Next
Set rngSourceCell = Nothing
Next
Set wsSource = Nothing
Set wsDestination = Nothing
End Sub
----------------------------------------------------------------------
以上のようなコードを実行できればよい、ということでしょうか。