我想知道如何搜索VBA模块中的每一行,并获得找到文本的任何行的数目。我想出了这样的办法:
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参数类型不匹配:
If vbCode.Find(strSearchPhrase, i, 1, -1, -1) Then还有其他人想怎么做吗?
发布于 2015-06-03 09:48:48
也许将Dim i As Integer更改为Dim i As Long将解决不匹配错误?
CodeModule对象有一个查找方法,您可以使用该方法在代码模块中搜索文本。Find方法接受ByRef长参数。
源-> http://www.cpearson.com/excel/vbe.aspx
https://stackoverflow.com/questions/30614760
复制相似问题