Finding the line number of code using code with VBA/VBIDE

Kmanbozzy

New Member
Joined
Apr 18, 2016
Messages
18
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?

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:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Kmanbozzy,

You might consider the following...

Code:
Sub vbeLine_1065420()
Dim VBComp As VBIDE.VBComponent
Dim n As Long, s As String

With VBComp.CodeModule
    For n = 1 To .CountOfLines
        s = .Lines(n, 1)
        If Trim(s) = vbNullString Then
            ' blank line, skip it
        ElseIf Left(Trim(s), 1) = "'" Then
            ' comment line, skip it
        Else
            Exit For
        End If
    Next n
End With
MsgBox "Code starts at line number " & n
End Sub

Please note, code is untested.

Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top