0

I'm trying to create a macro that can compare the same macro from 2 different files, and get me the diferences, similar to a local GitHub but on a smaller scale.

Fist step is done, that is to get a TreeView with all Modules grouped by type, and on Node Click, load all macros in a listview, with Start row, End Row, row Count.

What I'm trying to do with the macro below is to give it a line and a module name as variables, and the code should bring the target module in the activecodepane and select the macro from first to last row.

Unfortunately it's not doing that, it's actually selecting the line in the form code. Can you guys point out what I'm missing?

Public Sub VBA_SelectMacro(lLine As Long, Optional moduleName As String)

    'https://stackoverflow.com/questions/32381879/how-to-jump-to-line-number-in-vba-editor

    Dim lActiveLine  As Long
    Dim sProc As String
    Dim ProcType As Long
    Dim vbaModule As CodeModule
    Dim vbaPane As CodePane
    Dim startLine As Long
    
    Dim VBEProj As VBIDE.VBProject
    Dim vbComp As VBIDE.VBComponent
    Dim VBProj As VBIDE.VBProject
    
    'On Error Resume Next

    'lLine = Application.InputBox("Enter Line", "Go to Line", , , , , , 1)
    If moduleName <> "" Then
        'Set vbaPane = Application.VBE.CodePanes(moduleName)
        Set VBProj = ActiveWorkbook.VBProject
        Set vbComp = VBProj.VBComponents(moduleName)
        Set vbaModule = vbComp.CodeModule
        VBProj.VBComponents(moduleName).Activate
        vbaPane.SetSelection lLine, 1, lLine + 1, 1 'if i take out the on error resume next, i get the error "91: Object variable or With block variable not set"
    End If
    
    Set vbaPane = Application.VBE.ActiveCodePane
    Set vbaModule = vbaPane.CodeModule

    If lLine > 0 Then
        
        vbaPane.GetSelection lActiveLine, 0, 0, 0
        sProc = vbaModule.ProcOfLine(lActiveLine, vbext_pk_Proc)

        With vbaModule
            startLine = .ProcStartLine(sProc, ProcType)
            .CodePane.SetSelection startLine + lLine, 1, startLine + lLine + 1, 1
        End With
    End If

End Sub
Dumitru Daniel
  • 571
  • 4
  • 19

2 Answers2

2

This worked for me:

Dim lLine As Long, moduleName
Dim VBEProj As VBIDE.VBProject

lLine = 5
moduleName = "Module2"

Set VBEProj = ActiveWorkbook.VBProject

With VBEProj
    .VBComponents("Module2").Activate
    .VBE.ActiveCodePane.SetSelection lLine, 1, lLine + 1, 1
End With
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
0

Just to have a complete answer, I used @Tim Williams 's answer, but i needed to add DoEvents and Application.Wait, otherwhise the Code selection would go back to the From from where I run the code.

Public Sub VBA_SelectMacro(firstLine As Long, Optional lastLine As Long, Optional moduleName As String)

    'https://stackoverflow.com/questions/32381879/how-to-jump-to-line-number-in-vba-editor
    'https://stackoverflow.com/questions/67198778/vba-go-to-macro-and-line/67208211#67208211

    Dim VBProj As VBIDE.VBProject
    
    If moduleName <> "" Then
        Set VBProj = ActiveWorkbook.VBProject
        With VBProj
            .VBComponents(moduleName).Activate
            If lastLine = 0 Then: lastLine = firstLine + 1
            .VBE.ActiveCodePane.SetSelection firstLine, 1, lastLine, 1
            
            DoEvents
            Application.Wait (Now + TimeValue("0:00:01"))
        End With
    End If
End Sub
Dumitru Daniel
  • 571
  • 4
  • 19