VBA macro to extract parent part# for each part from a multi-level list

niranjanbt

New Member
Joined
Oct 10, 2013
Messages
5
Dear All,

I am trying to create a VBA macro which can extract parent part# for each part from a multi-level list.

In the example shown below:
Column-A = Muilti-level list AKA item#
Column-B = Part#
Column-C/D/E = Column-A split at dot delimiters
Column-F = Required output (parent part#) from the macro. Hope it is self-explanatory as to what is the parent part#

x5o1ec.jpg


Any help to develop this macro is much appreciated! :)

Thanks,
NT
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
NT,

In future, please post your data as data rather than a pic. Please see my signature regarding how to post your Excel data.

That said, you might consider the following...

Code:
Sub ParentPartNo_1025580()
Dim r As Range, Lengthh As Long, i As Long
For Each r In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    i = 1
    Lengthh = Len(r)
    Select Case Lengthh
        Case 1
            r.Offset(0, 5).Value = "-"
        Case 3
            Do
                If Len(Cells(r.Row - i, 1)) = 1 Then
                    r.Offset(0, 5).Value = Cells(r.Row - i, 2).Value
                    GoTo Nexxt
                End If
                i = i + 1
            Loop
        Case 5
            Do
                If Len(Cells(r.Row - i, 1)) = 3 Then
                    r.Offset(0, 5).Value = Cells(r.Row - i, 2).Value
                    GoTo Nexxt
                End If
                i = i + 1
            Loop
    End Select
Nexxt:
Next r
End Sub

Cheers,

tonyyy
 
Upvote 0
Another version, a bit more compact and allows for additional levels in part numbers (eg, 3.1.1.1)...

Code:
Sub ParentPartNo_1025580r2()
Dim r As Range, Lengthh As Long, i As Long
For Each r In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    i = 1
    Lengthh = Len(r)
    If Lengthh = 1 Then
        r.Offset(0, 5).Value = "-"
        GoTo Nexxt
    End If
    Do
        If Len(Cells(r.Row - i, 1)) = Lengthh - 2 Then
            r.Offset(0, 5).Value = Cells(r.Row - i, 2).Value
            GoTo Nexxt
        End If
        i = i + 1
    Loop
Nexxt:
Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,734
Messages
6,174,186
Members
452,550
Latest member
southernsquid2

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