Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for the text in VBA code (in which line)

I wonder how to search each line in VBA module and get the number of any line where text was found. I've figured out something like this:

Sub addProcedure()

Dim vbProj As VBIDE.VBProject
Dim vbComp As VBIDE.VBComponent
Dim vbCode As VBIDE.CodeModule

Dim strSearchPhrase As String
Dim strModuleName As String
Dim intLinesNr As Integer
Dim i As Integer
Dim intFoundLine As Integer

strModuleName = "Test"

Set vbProj = ActiveWorkbook.VBProject
Set vbComp = vbProj.VBComponents(strModuleName)
Set vbCode = vbComp.CodeModule

intLinesNr = vbCode.CountOfLines


For i = 1 To intLinesNr
  If vbCode.Find(strSearchPhrase, i, 1, -1, -1) Then
    intFoundLine = i
    Exit For
  End If
Next i

If foundline <> 0 Then MsgBox "Text found in " & intFoundLine & " line."


Set vbComp = Nothing
Set vbProj = Nothing
Set vbCode = Nothing
End Sub

And it returns Compile error: ByRef argument type mismatch in:

If vbCode.Find(strSearchPhrase, i, 1, -1, -1) Then

Any others ideas how to do that?

like image 345
Michał Plich Avatar asked Nov 20 '25 22:11

Michał Plich


1 Answers

Maybe change Dim i As Integer to Dim i As Long will resolve mismatch error?

The CodeModule object has a Find method that you can use to search for text within the code module. The Find method accepts ByRef Long parameters.

source -> http://www.cpearson.com/excel/vbe.aspx

like image 79
Dawid Avatar answered Nov 22 '25 17:11

Dawid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!