PDA

View Full Version : line of code



lior03
12-08-2005, 12:44 PM
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
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
thanks
moshe

mdmackillop
12-08-2005, 01:26 PM
Hi Moshe,
Try the following
Regards
MD


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

mvidas
12-08-2005, 01:27 PM
Hi Moshe,

There are a couple methods you could use. If you wanted to pass the workbook name to the function: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 FunctionOr if you just wanted it for ThisWorkbook (or ActiveWorkbook), use:Function LinesOfCode() As Long
Dim vbc As Object
For Each vbc In ThisWorkbook.VBProject.VBComponents
LinesOfCode = LinesOfCode + vbc.CodeModule.CountOfLines
Next
End FunctionMatt

lior03
12-09-2005, 01:17 AM
hello
i was thinking more or less about this
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
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

mvidas
12-09-2005, 07:51 AM
Hi moshe,

If you're looking for individual module (or individual component) count, you could use 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 FunctionMatt