在VBA代码中search文本(在哪一行中)
我不知道如何searchVBA模块中的每一行,并获得任何文本被发现的行的数量。 我已经想出了这样的事情:
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
并且它返回编译错误:ByRef参数types不匹配:
If vbCode.Find(strSearchPhrase, i, 1, -1, -1) Then
任何其他的想法如何做到这一点?
也许改变Dim i As Integer
Dim i As Long
将解决不匹配的错误?
CodeModule对象有一个Find方法,可以用来在代码模块中search文本。 Find方法接受ByRef Long参数。