title: Finding nested duplicates
body: <p>I am trying to make a Bill of Material list from my CAD software(Creo), this is exported out as a text file and its almost working perferct. The problem is that the text file spits out all parts in model, and I would like to count up duplicates instead of list them out after each other i.e quanties larger then 1.</p>
  <p>My code does this nicely if the duplicate is listed in the row above, however if its nested(sub assy) this does not work. In picture below 00151564.asm(level 2) should be listed as quantity=3 but the parts on level 3 should be listed as quantity=1</p>
  <p><a href="https://i.stack.imgur.com/hWdK6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hWdK6.png" alt="enter image description here" /></a></p>
  <p>Below is the original textfile creo spitts out, groups and patterns makes extra unwanteded indents, and material is just sometimes given..</p>
  <p><a href="https://i.stack.imgur.com/7WMh7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7WMh7.png" alt="enter image description here" /></a></p>
  <p>Here is my code:</p>
  <pre><code>Sub simen2(Optional myFile As String = &quot;Z:\Prosjekt\33907\Equipment and 
  materials\Structure\treetool2.txt&quot;)
  Dim text As String
  Dim textline As String
  Dim textlineTemp As String
  Dim foo As String
  Dim output As String
  Dim parent As String
  Dim grandma As String
  Dim greatgrandma As String
  Dim greatgreatgrandma As String
  Dim partNumber As String
  Dim quantity As Integer
  Dim material As String
  Dim wsOut As Worksheet
  Dim i, k As Long
  Dim level, levelOld, levelTemp, levelTempOld, subtractLevel As Integer
  Dim duplicate As Boolean
  Dim levelDictionary As Object
  'Init variables
  
  Set wsOut = ThisWorkbook.Worksheets(&quot;Output&quot;)
  
  subtractLevel = 0
  quantity = 1
  duplicate = True
  
  partNumberOld = &quot;&quot;
  commonNameOld = &quot;&quot;
  levelOld = 0
  levelTemp = 0
  levelTempOld = 0
  materialOld = &quot;&quot;
  textlineOld = &quot;&quot;
  material = &quot;NA&quot;
  materialOld = &quot;NA&quot;
  
  text = wsOut.Cells(1, 1).Value
  wsOut.Cells.ClearContents
  wsOut.Cells(1, 1).Value = text
  wsOut.Cells(1, 2).Value = Now
  wsOut.Cells(1, 3).Value = myFile
  
  
  Call write2ExcelHeader(wsOut)
  
  Set levelDictionary = CreateObject(&quot;Scripting.Dictionary&quot;)
  
  
  i = 0
  k = 1
  
  
  FileNum = FreeFile()
  Open myFile For Input As #FileNum
  
  Line Input #FileNum, foo
  Line Input #FileNum, foo
  
  
  Do Until EOF(FileNum)
      k = k + 1
      
      ' read in
      Line Input #FileNum, textline
      
      ' Get level, however group and pattern fuck things up
      If InStr(10, textline, &quot;&lt;HTML&gt;&quot;) &gt; 0 Or InStr(textline, &quot;Pattern&quot;) &gt; 0 Or InStr(textline, &quot;Group&quot;) &gt; 0 Then
          levelTemp = getLevel(textline)
          
          If levelTemp &lt; levelTempOld Then
              subtractLevel = 0
          End If
          
          If InStr(textline, &quot;Pattern&quot;) &gt; 0 Or InStr(textline, &quot;Group&quot;) &gt; 0 Then
              subtractLevel = subtractLevel + 1
              k = 1
          End If
      End If
      
      ' Grab material
      If InStr(textline, &quot;Materials&quot;) &gt; 0 Then
          Line Input #FileNum, textline
          material = Trim(Replace(textline, &quot;&lt;curr&gt;&quot;, &quot;&quot;))
      End If
      
      'we need to find out if the line has number as first item, i.e trim away spaces, it prints out previous item here...
      If InStr(10, textline, &quot;&lt;HTML&gt;&quot;) &gt; 0 Then
          'textlineTemp = RemoveHTML(textline)
          'textlineTemp = Replace(textlineTemp, &quot;Ã&quot;, &quot;Ø&quot;)
          
          
          partNumber = getPartNumber(textline)
          commonName = getCommonName(textline)
          partType = getType(partNumber)
          material = &quot;NA&quot;
              
          
          ' add part to dictionary, this is unique parts
          If levelDictionary.exists(partNumber) Then
              levelDictionary(partNumber) = levelDictionary(partNumber) + 1
          Else
              levelDictionary.Add partNumber, 1
          End If
          
          ' Remove duplicates ......
          If partNumberOld = partNumber And levelTempOld = levelTemp Then
              duplicate = True
              quantity = quantity + 1
          Else
              quantity = 1
              duplicate = False
          End If
          
  
  
          ' get family history
          level = levelTemp - subtractLevel
          LevelArray(level) = partNumber
          
       
          
          'lets present result
          If Not duplicate Then
              If level &gt; 1 Then
                  parent = LevelArray(level - 1)
              Else
                  parent = &quot;NA&quot;
              End If
              
              If level &gt; 2 Then
                  grandma = LevelArray(level - 2)
              Else
                  grandma = &quot;NA&quot;
              End If
              
              If level &gt; 3 Then
                  greatgrandma = LevelArray(level - 3)
              Else
                  greatgrandma = &quot;NA&quot;
              End If
              
              If level &gt; 4 Then
                  greatgreatgrandma = LevelArray(level - 4)
              Else
                  greatgreatgrandma = &quot;NA&quot;
              End If
              
              If i &gt; 0 Then
                   Call write2Excel(wsOut, i + 2, partNumberOld, commonNameOld, quantityOld, materialOld, levelOld, partTypeOld, parentOld, grandmaOld, greatgrandmaOld, greatgreatgrandmaOld)
              End If
              i = i + 1
          End If
          
            
      End If
      
  
      'we always uses previous values for print out
      partNumberOld = partNumber
      commonNameOld = commonName
      levelOld = level
      levelTempOld = levelTemp
      partTypeOld = partType
      quantityOld = quantity
      materialOld = material
      textlineOld = textline
      
      parentOld = parent
      grandmaOld = grandma
      greatgrandmaOld = greatgrandma
      greatgreatgrandmaOld = greatgreatgrandma
      
      
      
  Loop
  
   Close #FileNum
   Debug.Print &quot;How many parts &quot; &amp; i
  
   Call DeList(wsOut)
   Call CreateList(wsOut, &quot;FilterOutput&quot;)
  
  
   Call totalBOM(levelDictionary)
  End Sub
  </code></pre>