VBA: Insert new row based on condition

i8ur4re

Board Regular
Joined
Mar 1, 2015
Messages
97
Good day,

I have a list that looks something like this:

ItemNumberDescriptionCost
100-13Open Coil Innerspring Demo78
1020025Rocker Recliner/Pikara219.99
1020035Loveseat/Pikara/Gunsmoke254.99
1020038Sofa/Pikara/Gunsmoke269.99
1030025Rocker Recliner/Carnell/Brown259.99
1030087Reclining Power Sofa/Carnell519.99
1030088Reclining Sofa/Carnell/Brown419.99
1030094Double Rec Loveseat w/Console419.99
1030096DBL REC PWR Loveseat w/Console519.99
1030098Power Rocker Recliner/Carnell309.99

<tbody>
</tbody>


I have 3 columns, each column as shown above has the following headers; Item Number, Description, and Cost.

I would like to insert "Item Number" under each new set of numbers. A set of numbers is the first 3 numbers.
For example; The first "Item Number" 100-13 is one number. I would like to insert a "Item Number" under it so I can sort the new set of numbers which in this case would be "1020025-1020038".

The 102 is the primary number that this list must be sorted by. The next set is 103 (1030025-1030098). Under each set of numbers i would like to add a blank row so i can insert the headers that correspond to what that column indicates ("Item Number, Description, Cost").

At the end it should look something like this:

ItemNumberDescriptionCost
100-13Open Coil Innerspring Demo78
ItemNumberDescriptionCost
1020025Rocker Recliner/Pikara219.99
1020035Loveseat/Pikara/Gunsmoke254.99
1020038Sofa/Pikara/Gunsmoke269.99
ItemNumberDescriptionCost
1030025Rocker Recliner/Carnell/Brown259.99
1030087Reclining Power Sofa/Carnell519.99
1030088Reclining Sofa/Carnell/Brown419.99
1030094Double Rec Loveseat w/Console419.99
1030096DBL REC PWR Loveseat w/Console519.99
1030098Power Rocker Recliner/Carnell309.99

<tbody>
</tbody>


<tbody>
</tbody>


Its a very large list of items, over 6300 items to be exact. Macros are welcome.
ANY HELP WOULD BE APPRECIATED.
You guys are awesome. Thank you
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
If I understand... (and if your data is actually in columns A:C as your example implies), then perhaps something like this will get you closer.
Code:
Sub SeperateByFirst3()
Dim LstRw As Long, Rw As Long, Frst3 As String

''' (CHANGE 'A' TO THE COLUMN YOUR ITEM NUMBERS ARE REALLY IN)
LstRw = Cells(Rows.Count, "A").End(xlUp).Row
Frst3 = Left(Cells(LstRw, "A").Value, 3)

For Rw = LstRw To 2 Step -1
    If Not Left(Cells(Rw, "A").Value, 3) = Frst3 And Cells(Rw, "A") <> "" Then
        With Cells(Rw + 1, "A")
            .EntireRow.Insert
            Range("A1:C1").Copy .Offset(-1)
        End With
            Frst3 = Left(Cells(Rw, "A").Value, 3)
    End If
Next Rw

End Sub

Hope it helps.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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