Basically i want to use a macro to search another macro to return the line number of the particular code i'm looking for. The problem is, codemodule.find() has no other valid qualifiers to tag on the end such as .line or .row (codemodule.find().line) This is the general concept i would like to have answered.
As for my specific situation, i'm inserting a line of code underneath every single sub procedure, it works great, the only issue is if say the first macro in the module did not start on line 1. Like someone hit a few enter keys before they started the code. It's always the first macro in every module that it misplaces the inserted line of code only if the line did not start at 1. i mean i could manually insert this code or manually get rid of the spaces and it works but wheres the fun in that? plus i have a lot of macros i'm looping through and want to be able to reuse this macro in the future. So my thinking for a workaround is to get the line number the first procedure actually starts on and then +1 line to add the code there, then the rest of my macro will take care of the rest of the subs in that module. but i need to know how to find the line number the first sub actually starts on. and .procstartline always returns 1 for the first sub regardless of how many spaces have been entered. any help? maybe i need to think differently?
As for my specific situation, i'm inserting a line of code underneath every single sub procedure, it works great, the only issue is if say the first macro in the module did not start on line 1. Like someone hit a few enter keys before they started the code. It's always the first macro in every module that it misplaces the inserted line of code only if the line did not start at 1. i mean i could manually insert this code or manually get rid of the spaces and it works but wheres the fun in that? plus i have a lot of macros i'm looping through and want to be able to reuse this macro in the future. So my thinking for a workaround is to get the line number the first procedure actually starts on and then +1 line to add the code there, then the rest of my macro will take care of the rest of the subs in that module. but i need to know how to find the line number the first sub actually starts on. and .procstartline always returns 1 for the first sub regardless of how many spaces have been entered. any help? maybe i need to think differently?
Code:
Sub ListModules()
'for this to work you must go to tools References and click
'microsoft visual basics for application Extensibility 5.3
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Set VBProj = ActiveWorkbook.VBProject
For Each VBComp In VBProj.VBComponents
Set CodeMod = VBComp.CodeModule
If VBComp.CodeModule.CountOfLines > 0 Then
With CodeMod
LineNum = .CountOfDeclarationLines + 1
Do Until LineNum >= .CountOfLines
ProcName = .ProcOfLine(LineNum, 0)
If ProcName = "ListModules" Or ProcName = "Auto_Open" Then
GoTo g
End If
LineNum = LineNum + 1
.InsertLines LineNum, "Call Auto_Open"
g:
LineNum = .ProcStartLine(ProcName, 0) + _
.ProcCountLines(ProcName, 0)
Loop
End With
End If
Next VBComp
End Sub
Last edited: