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:

[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]40[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]42[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]5403[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]9519[/TD]
[/TR]
</tbody>[/TABLE]

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?

[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD][/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]40[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]40-1[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]40-2[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]40-3[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]40-4[/TD]
[/TR]
</tbody>[/TABLE]

And so on for all the variations.

Thanks!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
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,223,958
Messages
6,175,628
Members
452,661
Latest member
Nonhle

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