VBA code for CTE Recursive in SQL Data table

Joined
May 27, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have Data table which has data as shown below

Data table.PNG


In this I need to get the "Component" data which are structured under "Item" and after that each component will be considered as "Item" and recursive CTE comes here. Please help me to sort this out.

Sample Input:

Input.PNG


Sample Output

Output.PNG


in this Item is Assy. no and Component data need to be populated in Respective levels.

For example

"001-1194-8" will be given as Input, so it is considered as "Item" and 1st level data will be "Component" from data table. After that this "component" (001-1194-8FL) will be considered as "Item" and their respective "component" will be populated in adjacent cell and it has to be done till now item is found in "Item" data as shown in sample Output.

Thanks in advance.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I got something working strictly based on the info you provided. Not knowing where your data exists, I assumed that the input table started at A1, therefore, my code starts with A2 and works down. Also, I don't know where your output table is. I put H2 as a testing location, but you will have to change it to the right location.

There is no making sure the output table is blank or anything like that. I don't know what your data looks like when you want to start the Sub.
VBA Code:
Sub GenerateOutput()
    Dim sNo As Integer
    Dim output1(1 To 1, 1 To 5) As String
    Dim output2(1 To 1, 1 To 5) As String
    Dim output3(1 To 1, 1 To 5) As String
    Dim rSource As Range
    Dim rTarget As Range
    
    sNo = 0
    Set rSource = Range("A2")
    Set rTarget = Range("H2")
    
    Do While rSource.Value <> ""
        ClearOutputs output1, output2, output3
        sNo = sNo + 1
        output1(1, 1) = sNo
        output1(1, 2) = rSource.Offset(0, 2).Value
        output2(1, 1) = rSource.Offset(0, 4).Value
        output2(1, 3) = rSource.Offset(0, 5).Value
        output2(1, 5) = rSource.Offset(0, 3).Value
        rTarget.Resize(1, 5).Value = output1
        Set rTarget = rTarget.Offset(1, 0)
        rTarget.Resize(1, 5).Value = output2
        Set rSource = rSource.Offset(1, 0)
        Set rTarget = rTarget.Offset(1, 0)
        If rSource.Offset(0, 2).Value = output2(1, 3) Then
            output3(1, 1) = rSource.Offset(0, 4).Value
            output3(1, 4) = rSource.Offset(0, 5).Value
            output3(1, 5) = rSource.Offset(0, 3).Value
            rTarget.Resize(1, 5).Value = output3
            Set rTarget = rTarget.Offset(1, 0)
            Set rSource = rSource.Offset(1, 0)
        End If
    Loop
End Sub

Sub ClearOutputs(ByRef o1() As String, ByRef o2() As String, ByRef o3() As String)
    Dim i As Integer
    
    For i = 1 To 5
        o1(1, i) = ""
        o2(1, i) = ""
        o3(1, i) = ""
    Next
End Sub

I was able to produce the following:
CTE recursive.xlsm
ABCDEFGHIJKL
1CompanyClentItemqtypermtlseqComponent
2AA100001-1194-810030001-1194-8FL1001-1194-8
3AA100001-1194-8FL173.4340010NS101601210030001-1194-8FL1
4AA100001-1210-210030001-1210-2FL0010NS10160121173.434
5AA100001-1210-2FL3.8750010NS101601212001-1210-2
6AA100001-1228-40.650010NS061481210030001-1210-2FL1
7AA100001-1229-210030001-1229-2FL0010NS101601213.875
8AA100001-1229-2FL0.9350010NS061481213001-1228-4
90010NS061481210.65
104001-1229-2
110030001-1229-2FL1
120010NS061481210.935
Sheet1
 
Upvote 0
I got something working strictly based on the info you provided. Not knowing where your data exists, I assumed that the input table started at A1, therefore, my code starts with A2 and works down. Also, I don't know where your output table is. I put H2 as a testing location, but you will have to change it to the right location.

There is no making sure the output table is blank or anything like that. I don't know what your data looks like when you want to start the Sub.
VBA Code:
Sub GenerateOutput()
    Dim sNo As Integer
    Dim output1(1 To 1, 1 To 5) As String
    Dim output2(1 To 1, 1 To 5) As String
    Dim output3(1 To 1, 1 To 5) As String
    Dim rSource As Range
    Dim rTarget As Range
   
    sNo = 0
    Set rSource = Range("A2")
    Set rTarget = Range("H2")
   
    Do While rSource.Value <> ""
        ClearOutputs output1, output2, output3
        sNo = sNo + 1
        output1(1, 1) = sNo
        output1(1, 2) = rSource.Offset(0, 2).Value
        output2(1, 1) = rSource.Offset(0, 4).Value
        output2(1, 3) = rSource.Offset(0, 5).Value
        output2(1, 5) = rSource.Offset(0, 3).Value
        rTarget.Resize(1, 5).Value = output1
        Set rTarget = rTarget.Offset(1, 0)
        rTarget.Resize(1, 5).Value = output2
        Set rSource = rSource.Offset(1, 0)
        Set rTarget = rTarget.Offset(1, 0)
        If rSource.Offset(0, 2).Value = output2(1, 3) Then
            output3(1, 1) = rSource.Offset(0, 4).Value
            output3(1, 4) = rSource.Offset(0, 5).Value
            output3(1, 5) = rSource.Offset(0, 3).Value
            rTarget.Resize(1, 5).Value = output3
            Set rTarget = rTarget.Offset(1, 0)
            Set rSource = rSource.Offset(1, 0)
        End If
    Loop
End Sub

Sub ClearOutputs(ByRef o1() As String, ByRef o2() As String, ByRef o3() As String)
    Dim i As Integer
   
    For i = 1 To 5
        o1(1, i) = ""
        o2(1, i) = ""
        o3(1, i) = ""
    Next
End Sub

I was able to produce the following:
CTE recursive.xlsm
ABCDEFGHIJKL
1CompanyClentItemqtypermtlseqComponent
2AA100001-1194-810030001-1194-8FL1001-1194-8
3AA100001-1194-8FL173.4340010NS101601210030001-1194-8FL1
4AA100001-1210-210030001-1210-2FL0010NS10160121173.434
5AA100001-1210-2FL3.8750010NS101601212001-1210-2
6AA100001-1228-40.650010NS061481210030001-1210-2FL1
7AA100001-1229-210030001-1229-2FL0010NS101601213.875
8AA100001-1229-2FL0.9350010NS061481213001-1228-4
90010NS061481210.65
104001-1229-2
110030001-1229-2FL1
120010NS061481210.935
Sheet1


Hi shknbk2,

Thanks for your time.

Actually, the data that we receive from SQL Database is the Input. And code has to run through the data and have to produce the output as shown.
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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