Count all VBA code lines in Workbook (Code)


Greetings all,

     Initially Inspired by the_net_2.0 on, I wrote code to count how many lines of code are in your workbook (sheet/workbook events, userforms, modules ...). You can put this code in any Sub and it will work. I used it in a message box (MsgBox) to tell the user how many lines of code there are, among other information. Let me know what you think or if you get errors.


Sub CodeCounter()

On Error GoTo CodeLineCount_Err
Dim CodeLineCount As Double
Set CodeLineCount_Var = ThisWorkbook.VBProject

	For Each CodeLineCount_Var In CodeLineCount_Var.VBComponents
		CodeLineCount = CodeLineCount + CodeLineCount_Var.CodeModule.CountOfLines

CodeLineCount_Total = CodeLineCount

Set CodeLineCount_Var = Nothing
MsgBox CodeLineCount_Total

End Sub


2 Replies

Just a highlight that will be necessary the user enable the option "Trust access to the VBA project object model"

File" > "Options" > "Trust Center" > "Trust Center Settings"

And the library Microsoft Visual Basic For Applications Extensibility XX
Thank You Kendethar
But would you please revise this code with Option Explicit set on.

Georgie Aanne