Adding a new row based on comparing two cells

CamOne

New Member
Joined
Nov 13, 2022
Messages
1
Office Version
  1. 365
Platform
  1. MacOS
Hello,

Nice to meet everyone. Thank you in advance for taking a look at my post.

I have a large number of products in spreadsheets that are cataloged using item numbers. The item numbers are made of 3 parts, the design that will be printed on an item, the product type it will be printed on, and the size. Here are two example product numbers: PPC100-PFFH-1 and PPC100-PF-1. They both have "PPC100" because they both use the same design but the second set of characters is different because they will be printed on two separate product types (t-shirt, hoodie, etc.) The last part of the product number "-1" refers to the size of the garment (small, medium, large)

What I want to do:
  1. I want to automatically add a new row every time there is a new product
  2. In the item number cell of the new row, I want to add the item number minus the size for the item numbers below it

I have attached a couple of screenshots and have used XL2BB to copy the data from Excel. If anyone needs clarification, please let me know. Thanks in advance for looking at my post!


item list tester.xlsx
ABCD
1Internal IDItem NumberUPC CodeDescription
21PPC100-PFFH-18799523897property name/design name-ADULT PULL-OVER HOODIE-CHARCOAL-SM
32PPC100-PFFH-28799523898property name/design name-ADULT PULL-OVER HOODIE-CHARCOAL-MD
43PPC100-PFFH-38799523899property name/design name-ADULT PULL-OVER HOODIE-CHARCOAL-LG
54PPC100-PFFH-48799523900property name/design name-ADULT PULL-OVER HOODIE-CHARCOAL-XL
65PPC100-PFFH-58799523901property name/design name-ADULT PULL-OVER HOODIE-CHARCOAL-2X
76PPC100-PFFH-68799523902property name/design name-ADULT PULL-OVER HOODIE-CHARCOAL-3X
87PPC100-PF-18799523903property name/design name - L/S ADULT 18/1 - CHARCOAL - SM
98PPC100-PF-28799523904property name/design name - L/S ADULT 18/1 - CHARCOAL - MD
109PPC100-PF-38799523905property name/design name - L/S ADULT 18/1 - CHARCOAL - LG
1110PPC100-PF-48799523906property name/design name - L/S ADULT 18/1 - CHARCOAL - XL
1211PPC100-PF-58799523907property name/design name - L/S ADULT 18/1 - CHARCOAL - 2X
1312PPC100-PF-68799523908property name/design name- L/S ADULT 18/1 -CHARCOAL-3X
1413PPC100-SF-18799523909property name/design name - ADULT CREWNECK SWEATSHIRT - CHARCOAL - SM
1514PPC100-SF-28799523910property name/design name - ADULT CREWNECK SWEATSHIRT - CHARCOAL - MD
1615PPC100-SF-38799523911property name/design name - ADULT CREWNECK SWEATSHIRT - CHARCOAL - LG
1716PPC100-SF-48799523912property name/design name - ADULT CREWNECK SWEATSHIRT - CHARCOAL - XL
1817PPC100-SF-58799523913property name/design name - ADULT CREWNECK SWEATSHIRT - CHARCOAL - 2X
1918PPC100-SF-68799523914property name/design name - ADULT CREWNECK SWEATSHIRT - CHARCOAL - 3X
2019PPC100-TF-18799523915property name/design name - S/S ADULT 18/1 - CHARCOAL - SM
2120PPC100-TF-28799523916property name/design name - S/S ADULT 18/1 - CHARCOAL - MD
2221PPC100-TF-38799523917property name/design name - S/S ADULT 18/1 - CHARCOAL - LG
2322PPC100-TF-48799523918property name/design name - S/S ADULT 18/1 - CHARCOAL - XL
2423PPC100-TF-58799523919property name/design name - S/S ADULT 18/1 - CHARCOAL - 2X
2524PPC100-TF-68799523920property name/design name - S/S ADULT 18/1 - CHARCOAL - 3X
2625PPC100-TF-78799523921property name/design name-S/S ADULT 18/1 - CHARCOAL - 4X
2726PPC100-TF-88799523922property name/design name-S/S ADULT 18/1 - CHARCOAL - 5X
2827PPC100-TFF-48799523923property name/design name-S/S ADULT TALL-CHARCOAL-XL
2928PPC100-TFF-58799523924property name/design name-S/S ADULT TALL-CHARCOAL-2X
3029PPC100-TFF-68799523925property name/design name-S/S ADULT TALL-CHARCOAL-3X
3130PPC100-VF-18799523926property name/design name - S/S ADULT V-NECK - CHARCOAL - SM
3231PPC100-VF-28799523927property name/design name - S/S ADULT V-NECK - CHARCOAL - MD
3332PPC100-VF-38799523928property name/design name - S/S ADULT V-NECK - CHARCOAL - LG
3433PPC100-VF-48799523929property name/design name - S/S ADULT V-NECK - CHARCOAL - XL
3534PPC100-VF-58799523930property name/design name - S/S ADULT V-NECK - CHARCOAL - 2X
3635SR138-ST-18799523931property name/design name-S/S JUVENILE 18/1-ATHLETIC HEATHER-SM(4)
3736SR138-ST-28799523932property name/design name-S/S JUVENILE 18/1-ATHLETIC HEATHER-MD(5/6)
3837SR138-ST-38799523933property name/design name-S/S JUVENILE 18/1-ATHLETIC HEATHER-LG(7)
3938SR138-TF-18799523934property name/design name-S/S TODDLER TEE-ATHLETIC HEATHER-SM(2T)
4039SR138-TF-28799523935property name/design name-S/S TODDLER TEE-ATHLETIC HEATHER-MD(3T)
4140SR138-TF-38799523936property name/design name-S/S TODDLER TEE-ATHLETIC HEATHER-LG(4T)
4241SR138-SFFH-18799523937property name/design name-YOUTH PULL-OVER HOODIE - HEATHER - SM
4342SR138-SFFH-28799523938property name/design name-YOUTH PULL-OVER HOODIE - HEATHER - MD
4443SR138-SFFH-38799523939property name/design name-YOUTH PULL-OVER HOODIE - HEATHER - LG
4544SR138-SFFH-48799523940property name/design name-YOUTH PULL-OVER HOODIE - HEATHER - XL
4645SR138-FL-18799523941property name/design name-YOUTH LONG SLEEVE TEE-ATHLETIC HEATHER-SM
4746SR138-FL-28799523942property name/design name-YOUTH LONG SLEEVE TEE-ATHLETIC HEATHER-MD
4847SR138-FL-38799523943property name/design name-YOUTH LONG SLEEVE TEE-ATHLETIC HEATHER-LG
4948SR138-FL-48799523944property name/design name-YOUTH LONG SLEEVE TEE-ATHLETIC HEATHER-XL
5049SR138-TF-18799523945property name/design name-S/S YOUTH 18/1-ATHLETIC HEATHER-SM
5150SR138-TF-28799523946property name/design name-S/S YOUTH 18/1-ATHLETIC HEATHER-MD
5251SR138-TF-38799523947property name/design name-S/S YOUTH 18/1-ATHLETIC HEATHER-LG
5352SR138-TF-48799523948property name/design name-S/S YOUTH 18/1-ATHLETIC HEATHER-XL
DTG All Styles A - B
 

Attachments

  • Screen Shot 2022-11-13 at 6.04.40 PM copy.jpg
    Screen Shot 2022-11-13 at 6.04.40 PM copy.jpg
    160.7 KB · Views: 10
  • Screen Shot 2022-11-13 at 5.57.22 PM copy.jpg
    Screen Shot 2022-11-13 at 5.57.22 PM copy.jpg
    159 KB · Views: 11

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The formatting of the first row is a little wonky, but that can be cleaned up manually. I found the FindN function on a different website. It finds the location of the second "-" in the Item Number so the size can be removed.

VBA Code:
Sub newproduct()
    Dim r As Double                                     'current row
    Dim p As Integer                                    'position of second "-"
    Dim pnum As String                                  'product number without size
    
    Dim curritem As String                              'current item number without the size
    Dim nextitem As String                              'next item number without the size
    
    'the first row is a special case
    p = FindN("-", Cells(2, 2), 2)                      'find location of 2nd dash in the next row
    nextitem = Left(Cells(2, 2), p - 1)                 'set nextitem to all the chars before the 2nd dash
    Range("a2").EntireRow.Insert                        'Insert the row
    Cells(2, 2) = nextitem                              'put the item number in the cell
    Range(Cells(2, 2), Cells(2, 2)).Interior.Color = vbGreen    'favorite shade of green

    r = 2                                               'start row 2
    While Cells(r + 1, 2) <> ""                         'while there is more data in the next row
        
        p = FindN("-", Cells(r, 2), 2)                  'find location of the 2nd dash
        If p Then                                       'if there was a 2nd dash
            curritem = Left(Cells(r, 2), p - 1)         'set curritem to all of the chars before the 2nd dash
        
            p = FindN("-", Cells(r + 1, 2), 2)          'find location of 2nd dash in the next row
            nextitem = Left(Cells(r + 1, 2), p - 1)     'set nextitem to all the chars before the 2nd dash
        
            If curritem <> nextitem Then                'are the item numbers different?
                Range("a" & r + 1).EntireRow.Insert     'Insert the row
                Cells(r + 1, 2) = nextitem              'put the item number in the cell
                Range(Cells(r + 1, 2), Cells(r + 1, 2)).Interior.Color = vbGreen    'favorite shade of green
            End If
        End If

        r = r + 1                                       'move to the next row
    Wend
    
End Sub

Function FindN(sFindWhat As String, _
sInputString As String, N As Integer) As Integer
    Dim J As Integer
    Application.Volatile
    FindN = 0
    For J = 1 To N
        FindN = InStr(FindN + 1, sInputString, sFindWhat)
        If FindN = 0 Then Exit For
    Next
End Function
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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