How to count formula length not cell value length?

drmingle

Board Regular
Joined
Oct 5, 2009
Messages
229
Does anybody have a similiar code out there as the below which only counts the number of characters within formulas of a spreadsheet?

For example, cell A1 has the value of '310', but the formula to get the result of '310' is 431 characters in length.

Code:
Sub CountCodeLines()
     Dim VBCodeModule As Object
     Dim NumLines As Long, N As Long
     With ActiveWorkbook
           For N = 1 To .VBProject.VBComponents.Count
                 Set VBCodeModule = .VBProject.VBComponents(N).CodeModule
                 NumLines = NumLines + VBCodeModule.CountOfLines
           Next
     End With
     MsgBox "Total number of lines of code in the project = " & NumLines, , "Code Lines"
     Set VBCodeModule = Nothing
End Sub

I am using Excel 2010.
Thanks in advance.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Here's a UDF that will do what you want. Count includes the "=" at the start of the formula.
Code:
Function FormulaLen(R As Range) As Variant
Set R = R.Cells(1, 1)
If R.HasFormula Then
    FormulaLen = Len(R.Formula)
Else
    FormulaLen = "No formula in cell " & R.Address
End If
End Function
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top