In the next sample structure BOM:
we find articles, and further down we find their structure with its content, other articles which in their turn may or may not have their content and so on.
I'm trying to create a recursive routine to determine whether or not they exist further down and determine their level:
Public eof As Long
Sub RecursiveSearch()
Dim i As Long
Dim ws As Worksheet
Dim art_to_search As String
Dim str_art As Integer
Dim l As Integer ' Declare l as a local variable
Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with the actual sheet name
eof = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row 'Recalc end of file
'l is the level
l = 1
i = 1
' Loop through each row in the data range
For i = i To eof
art_to_search = Cells(i, 2).Value
str_art = Len(art_to_search)
If str_art = 15 Then
Debug.Print i & " " & art_to_search
Call ArticleExists(art_to_search, i + 1, eof, l)
End If
Next i
End Sub
Sub ArticleExists(ByVal article As String, ByVal startRow As Long, ByVal lastRow As Long, ByRef l As Integer)
Dim a As Long
' Loop through each row below the start row
For a = startRow To lastRow
If Trim(article) = Trim(Range("A" & a).Value) Then
l = l + 1
Range("D" & a) = l
Call ArticleExists(Cells(a, 2).Offset(2, 0), a + 2, lastRow, l)
Exit Sub
End If
Next a
End Sub
It works, but the counting of levels is not correct. Could you give me some help to improve my understanding of recursion on this problem?