copying cells based on a cell value

andysh

Board Regular
Joined
Nov 8, 2019
Messages
111
Hi,

I'm trying to achieve the following and hoping someone can help with the VBA.

I need to copy rows a number of times based on a cell quantity and then set the quantity in each row to one.

So something like:

for each row:
if (cell C) > 1:
copy row and insert (cell C - 1) times
change (cell C) in each row to 1

The outcome would change this:

aaaaaaaa3aaaaaaaaaaaa
bbbbbbbb2bbbbbbbbbbbb
cccccccc1cccccccccccc
dddddddd2dddddddddddd

into this:

aaaaaaaa1aaaaaaaaaaaa
aaaaaaaa1aaaaaaaaaaaa
aaaaaaaa1aaaaaaaaaaaa
bbbbbbbb1bbbbbbbbbbbb
bbbbbbbb1bbbbbbbbbbbb
cccccccc1cccccccccccc
dddddddd1dddddddddddd
dddddddd1dddddddddddd

Hope that makes sense and someone can help
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
When your data starts in A1:

VBA Code:
Sub jvr()
 ar = Sheets(1).Cells(1, 1).CurrentRegion
 
 With CreateObject("scripting.dictionary")
   For i = 1 To UBound(ar)
      For j = 1 To ar(i, 3)
        .Item(.Count) = Application.Index(ar, i, 0)
      Next
    Next
   Sheets(1).Cells(1, 1).Resize(.Count, UBound(ar, 2)) = Application.Index(.items, 0, 0)
  Sheets(1).Cells(1, 3).Resize(.Count) = 1
 End With
End Sub
 
Upvote 0
Solution
That's perfect, which row of code would I tweak to get it to start on row 3 and give me a couple of header rows?
 
Upvote 0
Apologies for bumping but could do with some help if anyone can answer the above question.
 
Upvote 0
Also change the last 2 lines of code. That is your "paste" destination
 
Upvote 0

Forum statistics

Threads
1,215,836
Messages
6,127,182
Members
449,368
Latest member
JayHo

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