Copy cells in a loop

kubabocz

New Member
Joined
Oct 28, 2015
Messages
36
Hi,

Iam struggling with VBA. I have data in column A and want it to be copied amount of times that is represented in corresponding cell in column B.
So going from part on the left to the one that is on right
1705498269557.png


Of course the number of items in column A can vary.

Thank you
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I think this works.

Public Sub CopyIt()
Dim rng As Range
Dim i As Integer, x As Integer
Dim arCnt As Integer: arCnt = 1
Dim copyX As Integer
Dim ar() As Variant

Set rng = Sheet1.Range("A1:B3") ' get your range

For i = 1 To rng.Rows.Count
copyX = rng.Cells(i, 2).Value

ReDim Preserve ar(1 To arCnt + copyX - 1)

For x = 1 To copyX
ar(arCnt) = rng.Cells(i, 1).Value
arCnt = arCnt + 1
Next
Next

Set rng = Sheet1.Range("F1")

Set rng = rng.Resize(UBound(ar, 1))

rng.Value = Application.Transpose(ar)

End Sub
 
Upvote 0
Depending on what you are trying to do and how big your dataset is. I'd read the whole range into an array first and work with that then once to the sheet.
 
Upvote 0
Here's a solution for you.

Option Explicit

VBA Code:
Sub CopyData()

Dim i As Long
Dim x As Long
Dim y As Long
Dim z As Long
Dim lr As Long
Dim CurrWS As Worksheet

Set CurrWS = ActiveSheet

lr = CurrWS.Cells(Rows.Count, "A").End(xlUp).Row  ' Determine last row of data in Column A
z = 1
For i = 1 To lr
    x = CurrWS.Cells(i, "B").Value
    For y = 1 To x
        CurrWS.Range("G" & z).Value = CurrWS.Range("A" & i).Value
        z = z + 1
    Next
Next

End Sub

Things to consider:
Variables i, x, y, z & lr can be defined as Integer instead of Long if there will be fewer that 32,767 rows of data.
 
Upvote 0
Here's a solution for you.

Option Explicit

VBA Code:
Sub CopyData()

Dim i As Long
Dim x As Long
Dim y As Long
Dim z As Long
Dim lr As Long
Dim CurrWS As Worksheet

Set CurrWS = ActiveSheet

lr = CurrWS.Cells(Rows.Count, "A").End(xlUp).Row  ' Determine last row of data in Column A
z = 1
For i = 1 To lr
    x = CurrWS.Cells(i, "B").Value
    For y = 1 To x
        CurrWS.Range("G" & z).Value = CurrWS.Range("A" & i).Value
        z = z + 1
    Next
Next

End Sub

Things to consider:
Variables i, x, y, z & lr can be defined as Integer instead of Long if there will be fewer that 32,767 rows of data.
This works perfectly, thank you so much :)
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top