Adding rows with a macro: Is this possible

racekarr

New Member
Joined
Jan 14, 2005
Messages
4
I have a spread sheet exported from Quick Books. The data is from purchase orders and inventory. Unfortunately, Quick Books exports the data of "quantity" as a number in the line item. To make price tags and inventory stickers, I need to take the "quantity" field in Excell and convert it to an equal number of rows. The reason I want to do this with a macro or other automated means is there can be 100's of rows of data with item quantities varying from 1 to more than 100. It gets very tedious copying and pasting in this manner. Here is what I would like to accomplish:
Book1
ABCD
1BEFORE
2
3ITEMDESCRIPTIONQTYPRICE
496-456BOLTA53.59
596-435BOLTB64.07
696-567BOLTC75.95
7
8
9AFTER
10
11ITEMDESCRIPTIONQTYPRICE
1296-456BOLTA13.59
1396-456BOLTA13.59
1496-456BOLTA13.59
1596-456BOLTA13.59
1696-456BOLTA13.59
1796-435BOLTB14.07
1896-435BOLTB14.07
1996-435BOLTB14.07
2096-435BOLTB14.07
2196-435BOLTB14.07
2296-435BOLTB14.07
2396-567BOLTC15.95
2496-567BOLTC15.95
2596-567BOLTC15.95
2696-567BOLTC15.95
2796-567BOLTC15.95
2896-567BOLTC15.95
2996-567BOLTC15.95
Sheet1


Is it possible to do this with a macro by reading the quantity field and then automatically copying and pasting the correct numbers of rows, then change the quantity field to 1?

Thanks for any input.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
How about

Code:
Sub ExtendRows()

Application.ScreenUpdating = False
Dim numRows As Integer
Dim r As Long
Dim rws As Long
Dim cls As Long
Dim Rng As Range
Dim LastRw As Long


LastRw = Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Range(Cells(1, "C"), Cells(LastRw, "C"))

  On Error Resume Next
  For r = Rng.Rows.Count To 2 Step -1
     If Rng.Rows(r).Offset(0, -2) <> "" Then
        numRows = Rng.Rows(r).Value - 1
        Rng.Rows(r + 1).Resize(numRows).EntireRow.Insert
        For rws = 1 To numRows
            For cls = 1 To 4
                Cells(r, cls).Offset(rws, 0).Value = Cells(r, cls).Value
                Cells(r, 3).Offset(rws, 0).Value = 1
            Next
        Next
        Cells(r, 3).Value = 1
     Else: Exit For
     End If
  Next r

Application.ScreenUpdating = True

End Sub

Ken.......
 
Upvote 0
Hi, racekarr,
Welcome to the Board!

this is working for me
Code:
Option Explicit

Sub adding_rows()
Dim rrr As Long
Dim fr As Long
Dim nr As Integer
Dim fc As Integer
Dim lc As Integer
Dim rc As Integer
fr = 2 'first row
fc = 1 'first column
lc = 4 'last column
rc = 3 'reference column

Application.ScreenUpdating = False
For rrr = Cells(65536, 1).End(xlUp).Row To fr Step -1
nr = Cells(rrr, rc) - 1
Rows(rrr + 1 & ":" & rrr + nr).Insert
Range(Cells(rrr, fc), Cells(rrr, lc)).Copy Range(Cells(rrr, fc), Cells(rrr + nr, lc))
Next rrr
Range(Cells(fr, rc), Cells(65536, rc).End(xlUp)) = 1
Application.ScreenUpdating = True
End Sub
is it for you ?
Step through the code with F8: so you will learn how things work ...

regards,
Erik
 
Upvote 0
racekarr said:
To make price tags and inventory stickers, I need to take the "quantity" field in Excell and convert it to an equal number of rows.

How are you printing your tags/stickers?

Perhaps it would be better to have a macro that prints the necessary number of tags/stickers per item without having to make any changes to your worksheet.
 
Upvote 0
THANK YOU!!!!! Works like a champ KenWright. You are the man!
Eric, I couldn't get your code to work. I am sure it is because I don't have the knowledge to adapt it to my application. Thanks for the effort both of you. I now know where I can turn with my questions.

Thanks again.
 
Upvote 0
Ponsonby,

I am using Microsoft Word to print the stickers through the mail merge feature using the Excel Spreadsheet as the data source.
 
Upvote 0
I agree with Ponsonby: This is perhaps a good example of cases were an analysis of the situation avoids some "useless" work.
Sometimes we try to do what people ask, without looking to what they try to achieve. just like inventing the wheel.
Racekarr, perhaps Ponsonby showed us where the wheel was :)

kind regards,
succes with your application
Erik
 
Upvote 0

Forum statistics

Threads
1,203,398
Messages
6,055,168
Members
444,767
Latest member
bryandaniel5

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