title: VBA - Find all numbered lines in VBE Modules via pattern search
body: <p><strong>Task:</strong> </p>
  
  <p>My goal is to find all <i>numbered lines</i> in procedures of my Code Modules. 
  The <em>CodeModule.Find</em> method can be used to check for search terms (target parameter). </p>
  
  <p><strong>Syntax:</strong> </p>
  
  <p>object.<strong>Find</strong>(target, startline, startcol, endline, endcol [, wholeword] [, matchcase] [, <strong>patternsearch</strong>])</p>
  
  <p>The referring help site <a href="https://msdn.microsoft.com/en-us/library/aa443952(v=vs.60).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/aa443952(v=vs.60).aspx</a> states:
  parameter <strong>patternsearch</strong>: Optional. A Boolean value specifying whether or not the target string is a regular expression pattern.
  If True, the target string is a regular expression pattern. False is the default.</p>
  
  <p>As explained above the find method allows a regex pattern search, which I would like to use in order to identify numbered lines in a precise way:
  digits followed by a tab. The example below therefore defines a search string s and sets the last parameter PatternSearch in the .Find method to True. </p>
  
  <p><strong>Problem</strong>
  AFAIK a valid regex definition could be </p>
  
  <pre><code>s = "[0-9]{1,4}[ \t]"
  </code></pre>
  
  <p>but that doesn't show anything, not even an error.</p>
  
  <p>In order to show at least any results, I defined the search term </p>
  
  <pre><code>s = "[0-9]*[ \t]*)"
  </code></pre>
  
  <p>in the calling example procedure <em>ListNumberedLines</em> showing erratic results.</p>
  
  <p><strong>Question</strong></p>
  
  <p>Is there any possibility to use a valid regex patternsearch in the CodeModule.Find method?</p>
  
  <p><strong>Example code</strong> </p>
  
  <pre><code>Option Explicit
  
  ' ==============
  ' Example Search
  ' ==============
  Sub ListNumberedLines()
  '  Declare search pattern string s
     Dim S As String
  10  S = "[0-9]*[ \t]*)"     
  20  Debug.Print "Search Term: " &amp; S
  30  Call findWordInModules(S)
  
  End Sub
  
  Public Sub findWordInModules(ByVal sSearchTerm As String)
  ' Purpose: find modules ('components') with lines containing a search term
  ' Method:  .CodeModule.Find with last parameter patternsearch set to True
  ' Based on https://www.devhut.net/2016/02/24/vba-find-term-in-vba-modulescode/
  
  ' VBComponent requires reference to Microsoft Visual Basic for Applications Extensibility
  '             or keep it as is and use Late Binding instead
  ' Declare module variable oComponent
    Dim oComponent            As Object    'VBComponent
  
    For Each oComponent In Application.VBE.ActiveVBProject.VBComponents
      If oComponent.CodeModule.Find(sSearchTerm, 1, 1, -1, -1, False, False, True) = True Then
          Debug.Print "Module: " &amp; oComponent.Name  'Name of the current module in which the term was found (at least once)
          'Need to execute a recursive listing of where it is found in the module since it could be found more than once
          Call listLinesinModuleWhereFound(oComponent, sSearchTerm)
      End If
    Next oComponent
  End Sub
  
  Sub listLinesinModuleWhereFound(ByVal oComponent As Object, ByVal sSearchTerm As String)
  ' Purpose: list module lines containing a search term
  ' Method:  .CodeModule.Find with last parameter patternsearch set to True
    Dim lTotalNoLines         As Long   'total number of lines within the module being examined
    Dim lLineNo               As Long   'will return the line no where the term is found
      lLineNo = 1
    With oComponent         ' Module
      lTotalNoLines = .CodeModule.CountOfLines
      Do While .CodeModule.Find(sSearchTerm, lLineNo, 1, -1, -1, False, False, True) = True
          Debug.Print vbTab &amp; "Zl. " &amp; lLineNo &amp; "|" &amp; _
                      Trim(.CodeModule.Lines(lLineNo, 1))  'Remove any padding spaces
          lLineNo = lLineNo + 1    'Restart the search at the next line looking for the next occurence
      Loop
    End With
  End Sub
  </code></pre>