Consulting

Results 1 to 5 of 5

Thread: line of code

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    line of code

    hello
    is it possible to build a udf that will count how many line of code ther are in every module in a workbook?
    maybe
    [VBA] Function lineofcode(modulename)
    On Error Resume Next
    Dim i As Integer
    Dim x As Object
    Dim numcomponents As Integer
    x = ActiveWorkbook.VBProject
    Dim vbp
    vbp = ActiveWorkbook.VBProject
    numcomponents = vbp.vbcomponents.count
    For i = 1 To numcomponents
    lineofcode = vbp.vbcomponents(i).codemodule.countoflines
    Next i
    End Function
    [/VBA] thanks
    moshe
    moshe

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Moshe,
    Try the following
    Regards
    MD

    [VBA]
    Function CodeLines()
    Dim Tmp As Long
    Dim VBCodeMod As CodeModule
    Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
    For i = 1 To ThisWorkbook.VBProject.VBComponents.Count
    Set VBCodeMod = ThisWorkbook.VBProject.VBComponents(i).CodeModule
    With VBCodeMod
    Tmp = Tmp + .CountOfLines
    End With
    Next
    CodeLines = Tmp
    End Function
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi Moshe,

    There are a couple methods you could use. If you wanted to pass the workbook name to the function:[vba]Function LinesOfCode(ByVal WorkbookName As String) As Long
    Dim vbc As Object, WB As Workbook
    On Error Resume Next
    Set WB = Workbooks(WorkbookName)
    On Error GoTo 0
    If WB Is Nothing Then Exit Function
    For Each vbc In WB.VBProject.VBComponents
    LinesOfCode = LinesOfCode + vbc.CodeModule.CountOfLines
    Next
    End Function[/vba]Or if you just wanted it for ThisWorkbook (or ActiveWorkbook), use:[vba]Function LinesOfCode() As Long
    Dim vbc As Object
    For Each vbc In ThisWorkbook.VBProject.VBComponents
    LinesOfCode = LinesOfCode + vbc.CodeModule.CountOfLines
    Next
    End Function[/vba]Matt

  4. #4
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    line of code version 2

    hello
    i was thinking more or less about this
    [vba]Function LinesOfCode(ByVal WorkbookName As String, module As String) As Long
    Dim vbc As Object, WB As Workbook
    On Error Resume Next
    Set WB = Workbooks(WorkbookName).Sheets(module)
    On Error GoTo 0
    If WB Is Nothing Then Exit Function
    For Each vbc In WB.VBProject.VBComponents
    LinesOfCode = LinesOfCode + vbc.CodeModule.CountOfLines
    Next
    End Function[/vba]
    the user enter a wb name and then a module name
    to get an integer of how many lines of code there are in that module.
    it do not work
    why?
    thanks
    Last edited by mvidas; 12-09-2005 at 07:43 AM. Reason: Added vba tags [vba] before the code and [/vba] after
    moshe

  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi moshe,

    If you're looking for individual module (or individual component) count, you could use [vba]Function LinesOfCode(ByVal WorkbookName As String, ByVal ModuleName As String) As Long
    Dim vbc As Object, WB As Workbook
    On Error Resume Next
    Set WB = Workbooks(WorkbookName)
    Set vbc = WB.VBProject.VBComponents(ModuleName)
    On Error GoTo 0
    If Not vbc Is Nothing Then
    LinesOfCode = vbc.CodeModule.CountOfLines
    End If
    End Function[/vba]Matt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •