VBA required to duplicate rows dependant on quantity

TM74

Board Regular
Joined
Aug 2, 2007
Messages
132
Hi,

I'm working on some VBA to re-format some quotations to a more basic layout for data entry. The re-formatted sheet is very simple containing just the product code in column A and the quoted quantity in column B.

A colleague has asked me if the quantities could be ommitted but if the quantity is greater than 1 then it should duplicate the code underneath the original (duplicate once for a quantity of 2 or twice for a quantity of 3 and so on).

I'm not really even sure where to start with this - can anyone help?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Code:
Sub createdups()
Dim rng As Range

Set rng = Sheets("input").Range("A2", Sheets("input").Range("A60000").End(xlUp))

For Each Item In rng
    Counter = Item.Offset(, 1).Value
    For RepeatCount = 1 To Counter
        Sheets("output").Range("A60000").End(xlUp).Offset(1).Value = Item.Value
    Next RepeatCount
Next Item


End Sub


Current sheet is called Input - This looks like

Excel Workbook
AB
1ProductQty
2PART11
3PART25
4PART36
5PART47
6PART55
7PART61
8PART710
9PART85
10PART92
11PART103
12PART111
13PART122
14PART135
15PART146
16PART157
Input




Create a second sheet called Output - then run the code. (copy and paste into a module)

Let me know how it goes.
 
Upvote 0
That does look absolutely perfect. Just one problem - which was my fault - I forgot to mention that column B does not contain the quantities! Column C does - column B contains a customer number. I solved this by changing this:

Counter = Item.Offset(, 1).Value

To this:

Counter = Item.Offset(, 2).Value

The only thing I can't work out is how to get the output sheet to contain the values from both column A & B. Could you help with that part?
 
Upvote 0
Code:
Sub createdups()
Dim rng As Range

Set rng = Sheets("input").Range("A2", Sheets("input").Range("A60000").End(xlUp))

For Each Item In rng
    Counter = Item.Offset(, 2).Value
    For RepeatCount = 1 To Counter
    With Sheets("output").Range("A60000").End(xlUp).Offset(1)
        .Value = Item.Value
        .Offset(, 1).Value = Item.Offset(, 1).Value
    End With
    Next RepeatCount
Next Item


End Sub

Should work.


Make sure you clear the output sheet before running a second time ;)
 
Upvote 0

Forum statistics

Threads
1,203,174
Messages
6,053,918
Members
444,694
Latest member
JacquiDaly

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