Repeat cell value in N times using Excel VBA

Hello friends! we’ll be learning to generate the cell value N times based on cell value using Excel VBA. Easy to follow and reusable VBA code and Excel file is attached.

As can be seen in the image below. column A is having the values to repeat and column B with the number of times to repeat. The code is below to achieve the result.

Code

Sub copy()
'----variables
Dim lrow_d As Long
Dim lrow_s As Long
'-----Sheets
Dim s_sht As Worksheet
'-----Define Sheet names
Set s_sht = Worksheets("Repeat")
'--LastRow
lrow_s = s_sht.Cells(Rows.Count, 1).End(xlUp).Row
'---Creating repeating
s_sht.Range("D2:D1000000").Clear
For i = 2 To lrow_s
lrow_d = s_sht.Cells(Rows.Count, 4).End(xlUp).Row
s_sht.Range("A" & i).copy Destination:=s_sht.Range("D" & lrow_d + 1 & ":" & "D" & lrow_d + s_sht.Range("B" & i))
Next i
End Sub

Get the File here

Keep visiting Analytics Tuts for more tutorials.

Thanks for reading! Comment your suggestions and queries


Leave a Reply

Your email address will not be published. Required fields are marked *