Copy & insert row based on cell numbers

littlefish71

New Member
Joined
Mar 27, 2013
Messages
4
Hi Guys,

I have had some luck but my method had led me in a direction which is making things tricky. So I need some help!!

I have a table which appears below. Column C indicates the serial number of an item which has been checked. Column D indicates the last serial number which is checked in the range.

ie: item 'a' serial numbers 285 to 295 have been checked. What I require is row 2 to be copied and inserted on the next row and automatically index the serial number to 286 until it reaches the last serial number of 295.
ie: the macro inserts 11 rows of all the same data except for column C which indexes on each row. Then moves onto the next row ie: in this table it is row 3 is copied and repeated 4 times indexing 352 to 355 and so on. if there is only 1 serial number then it remains as is.
A</SPAN>
B</SPAN>
C</SPAN>
D</SPAN>
1</SPAN>
Prod date</SPAN>
Item Name</SPAN>
1st Item No.</SPAN>
Last Item No.</SPAN>
</SPAN>
2</SPAN>
01/01/2013</SPAN>
a</SPAN>
285</SPAN>
288</SPAN>
</SPAN>
3</SPAN>
01/01/2013</SPAN>
b</SPAN>
352</SPAN>
356</SPAN>
</SPAN>
4</SPAN>
01/01/2013</SPAN>
c</SPAN>
343</SPAN>
</SPAN>
5</SPAN>
01/01/2013</SPAN>
d</SPAN>
108</SPAN>
</SPAN>
6</SPAN>
01/01/2013</SPAN>
e</SPAN>
200</SPAN>
208</SPAN>
</SPAN>
7</SPAN>
01/01/2013</SPAN>
f</SPAN>
30</SPAN>
</SPAN>
8</SPAN>
01/01/2013</SPAN>
g</SPAN>
62</SPAN>
63</SPAN>
</SPAN>

<TBODY>
</TBODY>



Part Finished table
1</SPAN>
Prod date</SPAN>
Item Name</SPAN>
1st Item No.</SPAN>
Last Item No.</SPAN>
2</SPAN>
01/01/2013</SPAN>
a</SPAN>
285</SPAN>
288</SPAN>
3</SPAN>
01/01/2013</SPAN>
a</SPAN>
286</SPAN>
288</SPAN>
4</SPAN>
01/01/2013</SPAN>
a</SPAN>
287</SPAN>
288</SPAN>
5</SPAN>
01/01/2013</SPAN>
a</SPAN>
288</SPAN>
288</SPAN>
6</SPAN>
01/01/2013</SPAN>
b</SPAN>
352</SPAN>
356...</SPAN>

<TBODY>
</TBODY>
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
littlefish71,

Welcome to the MrExcel forum.

Sample raw data:


Excel 2007
ABCD
1Prod dateItem Name1st Item No.Last Item No.
21/1/2013a285288
31/1/2013b352356
41/1/2013c343
51/1/2013d108
61/1/2013e200208
71/1/2013f30
81/1/2013g6263
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Sheet1


After the macro:


Excel 2007
ABCD
1Prod dateItem Name1st Item No.Last Item No.
21/1/2013a285288
31/1/2013a286288
41/1/2013a287288
51/1/2013a288288
61/1/2013b352356
71/1/2013b353356
81/1/2013b354356
91/1/2013b355356
101/1/2013b356356
111/1/2013c343
121/1/2013d108
131/1/2013e200208
141/1/2013e201208
151/1/2013e202208
161/1/2013e203208
171/1/2013e204208
181/1/2013e205208
191/1/2013e206208
201/1/2013e207208
211/1/2013e208208
221/1/2013f30
231/1/2013g6263
241/1/2013g6363
25
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub ReorgData()
' hiker95, 03/27/2013
' http://www.mrexcel.com/forum/excel-questions/693794-copy-insert-row-based-cell-numbers.html
Dim r As Long, lr As Long, n As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = lr To 2 Step -1
  If Cells(r, 4) <> "" Then
    n = Cells(r, 4) - Cells(r, 3)
    If n > 0 Then
      Rows(r + 1).Resize(n).Insert
      Cells(r + 1, 1).Resize(n, 2).Value = Cells(r, 1).Resize(, 2).Value
      With Cells(r + 1, 3).Resize(n)
        .FormulaR1C1 = "=R[-1]C+1"
        .Value = .Value
      End With
      Cells(r + 1, 4).Resize(n).Value = Cells(r, 4).Value
    End If
  End If
Next r
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgData macro.
 
Upvote 0
littlefish71,

You are very welcome. Glad I could help.

Thanks for the feedback, and, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,203,642
Messages
6,056,507
Members
444,872
Latest member
Vishal Gupta

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