How can I automatically populate the VBA Editor with line numbers?

前端 未结 7 1644
甜味超标
甜味超标 2020-12-02 01:41

I want to have line numbers in my VBA code for debugging reasons. That will allow me to know where a particular error occurred.

Is there an automatic feature for thi

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 02:01

    This is not 100% tested, but using VBA extensibility you could do the following

    Sub line_number(strModuleName As String)
    
    Dim vbProj As VBProject
    Dim vbComp As VBComponent
    Dim cmCode As CodeModule
    Dim intLine As Integer
    
    Set vbProj = Application.VBE.ActiveVBProject
    Set vbComp = vbProj.VBComponents(strModuleName)
    Set cmCode = vbComp.CodeModule
    
    For intLine = 2 To cmCode.CountOfLines - 1
       cmCode.InsertLines intLine, intLine - 1 &  cmCode.Lines(intLine, 1)
       cmCode.DeleteLines intLine + 1, 1
    Next intLine
    
    End Sub
    

    This gives the results before and after as below, altering in this way is not recommended though.

提交回复
热议问题