Expand product codes to include sub-codes

MPH88

New Member
Joined
Sep 28, 2017
Messages
13
I have a product numbers in a column A like this:

A
140
242
35403
49519

<tbody>
</tbody>

Product numbers can be one to four numerals. Each product can have up to 9 sub-codes. For example: 40-1, 42-5, 9519-8.

Can I use a macro to quickly expand this list and give me all the possible sub-codes, like this?

C
140
240-1
340-2
440-3
540-4

<tbody>
</tbody>

And so on for all the variations.

Thanks!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this:
Code:
Sub ExpandCodes()

    Dim lr As Long, r As Long, s As Long
    Dim v As Variant
    
    Application.ScreenUpdating = False
    
'   Find last cell in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows backwards
    For r = lr To 1 Step -1
        v = Cells(r, "A")
        Rows(r + 1 & ":" & r + 8).Insert
        For s = 1 To 8
            Cells(r + s, "A") = v & "-" & s
        Next s
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
In case you might be interested, here is another macro that you can consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub CodesSubCodes()
  Dim Arr As Variant
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    Arr = Application.Transpose(Split(Join(Application.Transpose(Evaluate(Replace("IF({1},@&"" ""&@&""-1 ""&@&""-2 ""&@&""-3 ""&@&""-4 ""&@&""-5 ""&@&""-6 ""&@&""-7 ""&@&""-8 ""&@&""-9 "")", "@", .Address))), "")))
    With .Cells(1).Resize(UBound(Arr))
      .NumberFormat = "@"
      .Value = Arr
    End With
  End With
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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