excel - 搜索VBA代码中的文本(在哪一行)

标签 excel vba search line

我想知道如何搜索 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

还有其他想法如何做到这一点吗?

最佳答案

也许将 Dim i As Integer 更改为 Dim i As Long 可以解决不匹配错误?

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.

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

关于excel - 搜索VBA代码中的文本(在哪一行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30614760/

相关文章:

algorithm - 在包含重复值的已排序和未排序数组中搜索和插入操作的时间复杂度

excel - 压缩两个和的差的公式

vba - 使用用户表单从当前打开的工作簿中检索外部工作簿的信息

excel - VBA 在关闭私有(private)子之前运行代码两次

database - 在列表框中换行文本 - ms access 2010

vba - 根据第三列的值比较两列

vba - 无法使 vlookup 工作

excel - 单击单元格时的操作

python - 如何在 linux 中使用 python 搜索可执行文件?

c# - 从列表框中搜索和删除项目