Split Ranges of Reference Designators

vdevita

New Member
Joined
Jul 13, 2012
Messages
6
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:
[TABLE="class: grid, width: 375, align: left"]
<tbody>[TR]
[TD]Part Number
[/TD]
[TD]Description
[/TD]
[TD]Reference Designator
[/TD]
[/TR]
[TR]
[TD]852852
[/TD]
[TD]Resistor
[/TD]
[TD]R1-R4
[/TD]
[/TR]
[TR]
[TD]258258
[/TD]
[TD]Capacitor
[/TD]
[TD]C16-C20
[/TD]
[/TR]
</tbody>[/TABLE]






This is how I would like it split:

[TABLE="class: grid, width: 375"]
<tbody>[TR]
[TD]Part Number
[/TD]
[TD]Description
[/TD]
[TD]Reference Designator
[/TD]
[/TR]
[TR]
[TD]852852
[/TD]
[TD]Resistor
[/TD]
[TD]R1
[/TD]
[/TR]
[TR]
[TD]852852
[/TD]
[TD]Resistor
[/TD]
[TD]R2
[/TD]
[/TR]
[TR]
[TD]852852
[/TD]
[TD]Resistor
[/TD]
[TD]R3
[/TD]
[/TR]
[TR]
[TD]852852
[/TD]
[TD]Resistor
[/TD]
[TD]R4
[/TD]
[/TR]
[TR]
[TD]258258
[/TD]
[TD]Capacitor
[/TD]
[TD]C16
[/TD]
[/TR]
[TR]
[TD]258258
[/TD]
[TD]Capacitor
[/TD]
[TD]C17
[/TD]
[/TR]
[TR]
[TD]258258
[/TD]
[TD]Capacitor
[/TD]
[TD]C18
[/TD]
[/TR]
[TR]
[TD]258258
[/TD]
[TD]Capacitor
[/TD]
[TD]C19
[/TD]
[/TR]
[TR]
[TD]258258
[/TD]
[TD]Capacitor
[/TD]
[TD]C20
[/TD]
[/TR]
</tbody>[/TABLE]

Thanks,
vdevita
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Where do you want the result?

and will the reference always start with an alphabet and end with a number?
 
Upvote 0
Code posted in the thread is fine with me.

Yes, the reference designator is always of that format. It will always begin with a letter and end with a number. Though there are a few cases that start with two letters then end with any sequence of numbers (AR101, VR202).
 
Upvote 0
Given that your original data is in column A to C starting from Row 1.
Code:
Sub a()
    Dim LR&, i&, startRow&, endRow&, strStart$, strEnd$, intDashPos%, tmp$, j&, strAlpha$
    Dim count&
    LR = Range("C" & Rows.count).End(xlUp).Row
    
    count = 2
    For i = 2 To LR
        intDashPos = InStr(1, Range("C" & i).Value, "-", vbBinaryCompare)
        If intDashPos Then
            strStart = Left(Range("C" & i).Value, intDashPos - 1)
            strEnd = Right(Range("C" & i).Value, Len(Range("C" & i).Value) - intDashPos)
            
            tmp = getAlpha(strStart)
            If Len(tmp) Then
                strAlpha = tmp
            Else
                MsgBox "Can not find alphabets in the reference" & Chr(10) & "Requires additional information about the reference format" & Chr(10) _
                    & "Failed on line " & i
                Exit Sub
            End If
            
            tmp = getNum(strStart)
            If Len(tmp) Then
                startRow = CLng(tmp)
            Else
                MsgBox "Can not find numbers in the reference" & Chr(10) & "Requires additional information about the reference format" & Chr(10) _
                    & "Failed on line " & i
                Exit Sub
            End If
            
            tmp = getNum(strEnd)
            If Len(tmp) Then
                endRow = CLng(tmp)
            Else
                MsgBox "Can not find numbers in the reference" & Chr(10) & "Requires additional information about the reference format" & Chr(10) _
                    & "Failed on line " & i
                Exit Sub
            End If
            
            'Copy Header
            Range("E1:G1").Value = Range("A1:C1").Value
            
            For j = startRow To endRow
                Range("E" & count).Value = Range("A" & i).Value
                Range("F" & count).Value = Range("B" & i).Value
                Range("G" & count).Value = strAlpha & j
                count = count + 1
            Next j
        Else
            MsgBox "Can not find ""-"" character in the reference" & Chr(10) & "Requires additional information about the reference format" & Chr(10) _
                    & "Failed on line " & i
            Exit Sub
        End If
    Next i
End Sub

Function getAlpha(str$)
    Dim i&
    getAlpha = ""
    For i = 1 To Len(str)
        If Asc(Mid(str, i, 1)) >= 48 And Asc(Mid(str, i, 1)) <= 57 Then
            getAlpha = Left(str, i - 1)
            Exit Function
        End If
    Next i
End Function

Function getNum(str$)
    Dim i&
    getNum = ""
    For i = 1 To Len(str)
        If Asc(Mid(str, i, 1)) >= 48 And Asc(Mid(str, i, 1)) <= 57 Then
            getNum = Right(str, Len(str) - i + 1)
            Exit Function
        End If
    Next i
End Function

The following code will print the results in column E to G starting from Row 1.

Hope it helps
 
Upvote 0
I have a similar situation I hope you can help with.
Instead of the reference designator being R1-R4, I have R1-4.
Can the same code be used for this situation?
Thank you
 
Upvote 0

Forum statistics

Threads
1,223,365
Messages
6,171,654
Members
452,415
Latest member
mansoorali

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