Split Ranges in Excel by Reference Designators that has "-" and ","

deejay1234

New Member
Joined
Oct 2, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have a worksheet of parts that contains ranges of reference designators, per part, that I would like split into individual reference designators.

For Example, here is a sample of the reference designator ranges:
Part NumberDescriptionReference Designator
15123030CAP CHIP 100PF 10% 50V X7R 0402C105,C503-C505
21723266CONN MCX JACK STR SMDJ600,J602-J603
This is how I would like it split:

Part NumberDescriptionReference Designator
15123030CAP CHIP 100PF 10% 50V X7R 0402C105
15123030CAP CHIP 100PF 10% 50V X7R 0402C503
15123030CAP CHIP 100PF 10% 50V X7R 0402C504
15123030CAP CHIP 100PF 10% 50V X7R 0402C505
21723266CONN MCX JACK STR SMDJ600
21723266CONN MCX JACK STR SMDJ602
21723266CONN MCX JACK STR SMDJ603

There was a same thread earlier but for some reason it does not work for me.

Thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi and welcome to MrExcel

Assuming the data starts in cell A2. The results in E2 onwards.

Try this macro:
VBA Code:
Sub Split_Ranges()
  Dim i As Long, j As Long, k As Long
  Dim num1, num2a, num2b, stxt
  
  k = 1
  For i = 2 To Range("A" & Rows.Count).End(3).Row
    For Each num1 In Split(Range("C" & i), ",")
      If InStr(1, num1, "-") > 0 Then
        With CreateObject("VBScript.RegExp")
          .Global = True
          .Pattern = "\D"
          num2a = .Replace(Split(num1, "-")(0), "")
          num2b = .Replace(Split(num1, "-")(1), "")
          stxt = Replace(Split(num1, "-")(0), num2a, "")
        End With
        For j = num2a To num2b
          k = k + 1
          Range("E" & k).Value = Range("A" & i).Value
          Range("F" & k).Value = Range("B" & i).Value
          Range("G" & k).Value = stxt & j
        Next
      Else
        k = k + 1
        Range("E" & k).Value = Range("A" & i).Value
        Range("F" & k).Value = Range("B" & i).Value
        Range("G" & k).Value = num1
      End If
    Next
  Next
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Solution
Hi and welcome to MrExcel

Assuming the data starts in cell A2. The results in E2 onwards.

Try this macro:
VBA Code:
Sub Split_Ranges()
  Dim i As Long, j As Long, k As Long
  Dim num1, num2a, num2b, stxt
 
  k = 1
  For i = 2 To Range("A" & Rows.Count).End(3).Row
    For Each num1 In Split(Range("C" & i), ",")
      If InStr(1, num1, "-") > 0 Then
        With CreateObject("VBScript.RegExp")
          .Global = True
          .Pattern = "\D"
          num2a = .Replace(Split(num1, "-")(0), "")
          num2b = .Replace(Split(num1, "-")(1), "")
          stxt = Replace(Split(num1, "-")(0), num2a, "")
        End With
        For j = num2a To num2b
          k = k + 1
          Range("E" & k).Value = Range("A" & i).Value
          Range("F" & k).Value = Range("B" & i).Value
          Range("G" & k).Value = stxt & j
        Next
      Else
        k = k + 1
        Range("E" & k).Value = Range("A" & i).Value
        Range("F" & k).Value = Range("B" & i).Value
        Range("G" & k).Value = num1
      End If
    Next
  Next
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Hi Dante,

That totally worked! If its not too much of a hassle could you include comments on the code so I could understand it a bit?

Thanks
 
Upvote 0
Before reviewing, I give a brief description of RegEx:
RegEx stands for "Regular Expression" in VBA Excel and is a sequence of characters that defines the search pattern for finding a specific pattern of characters in a string of values.

Code with comments:

VBA Code:
Sub Split_Ranges()
  
  'SECTION Variable declaration
  Dim i As Long, j As Long, k As Long
  Dim num1, num2a, num2b, stxt
  
  'Initial counter for the destination row
  k = 1
  
  'Cycle in column A, starting at 2 and going to the last row with data
  For i = 2 To Range("A" & Rows.Count).End(3).Row
  
    'For each item separated by comma
    For Each num1 In Split(Range("C" & i), ",")
    
      'Check if the item has "-"
      If InStr(1, num1, "-") > 0 Then
      
        'With RegEx it eliminates everything that is not a number.
        With CreateObject("VBScript.RegExp")
          .Global = True
          .Pattern = "\D"
          
          'If it contains "-" then it has 2 parts,
          'The left part goes to the variable num2a
          'The right part goes to the variable num2b
          
          'With the function Split and (0) you get the first part (before the "-")
          'With the function Split and (1) you get the second part (after the "-")
          num2a = .Replace(Split(num1, "-")(0), "")
          num2b = .Replace(Split(num1, "-")(1), "")
          
          ' Gets the letters that come before the number.
          stxt = Replace(Split(num1, "-")(0), num2a, "")
        End With
        
        'For each number from the initial number before the hyphen to the final number after the hyphen.
        For j = num2a To num2b
          'Increase the destination row
          k = k + 1
          
          'Pass values from column A and B to column E and F
          Range("E" & k).Value = Range("A" & i).Value
          Range("F" & k).Value = Range("B" & i).Value
          
          'Passes the concatenated value of the letters and consecutive to column G
          Range("G" & k).Value = stxt & j
        Next
      Else
      
        'If the item does not contain "-"
        k = k + 1
        
        'Pass values from column A and B to column E and F
        Range("E" & k).Value = Range("A" & i).Value
        Range("F" & k).Value = Range("B" & i).Value
        
        'Pass item to column G
        Range("G" & k).Value = num1
      End If
    Next
  Next
End Sub

😇
 
Upvote 0
A simple alternative with Power Query, aka Get and Transform Data and found on the Data Tab

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Reference Designator", Splitter.SplitTextByAnyDelimiter({",","-"}, QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Reference Designator")
in
    #"Split Column by Delimiter"

Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

- Follow this link to learn how to install Power Query in Excel 2010 / 2013.

- Follow this link for an introduction to Power Query functionality.

- Follow this link for a video which demonstrates how to use Power Query code provided.

Book2
ABC
1Part NumberDescriptionReference Designator
215123030CAP CHIP 100PF 10% 50V X7R 0402C105,C503-C505
321723266CONN MCX JACK STR SMDJ600,J602-J603
4
5Part NumberDescriptionReference Designator
615123030CAP CHIP 100PF 10% 50V X7R 0402C105
715123030CAP CHIP 100PF 10% 50V X7R 0402C503
815123030CAP CHIP 100PF 10% 50V X7R 0402C505
921723266CONN MCX JACK STR SMDJ600
1021723266CONN MCX JACK STR SMDJ602
1121723266CONN MCX JACK STR SMDJ603
Sheet1
 
Upvote 0

Forum statistics

Threads
1,223,362
Messages
6,171,639
Members
452,412
Latest member
MitchAgain

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