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

Kmanbozzy

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

tonyyy

Well-known Member
Joined
Jun 24, 2015
Messages
1,647
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
 

Forum statistics

Threads
1,082,144
Messages
5,363,381
Members
400,732
Latest member
robcooper2001

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top