Count the sum of the number of lines in a column

Ian Y

New Member
Joined
Nov 18, 2023
Messages
37
Office Version
  1. 2019
Platform
  1. Windows
In Google Sheets, if you want to count the sum of the number of lines in all cells in column A, this function can be used:

Code:
=counta(A1:A) + sum(arrayformula(len(regexreplace(A1:A, "[^\n]", ""))))

What’s the equivalent in Excel 2019? I tried the same function, and it doesn’t work and shows the result as #NAME?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I'm not sure exactly what you're after: sum (add) the values in column A; count the number of non-empty cells in column A? Something else?
 
Upvote 0
@kevin9999,

For example, if the first cell of column A has these 4 lines:
Code:
This is a line.
Yet another line.
How are you?
What time is it?

And the second cell of column A has these 3 lines:
Code:
More lines go here.
Today is sunday.
This year is 2023.
lines:

And one of the other cells of column A has these 1 line:
Code:
Just one more line.

Then the function I need would yield 8

The number of cells and lines inside is unpredictable.
 
Upvote 0
OK, it's the number of sentences in the column?
Each line can be a sentence, a url, a string of number, a word, etc. As long as it ends with a line break (when there are multiple lines in a cell), it needs to be counted as one line.
 
Upvote 0
It looks to me like it's a case of counting all the line breaks (plus 1 for each cell) in the entire range of interest. Not really my area, but hopefully someone else on the forum will come up with a solution for you 🤞
 
Upvote 0
With earlier versions of Excel you will probably need to use SumProduct.

T202311a.xlsm
ABCD
1
2ABC155
3ABC ABC2
4ABC ABC2
5
4i
Cell Formulas
RangeFormula
C2C2=SUM((LEN(A2:A4)-LEN(SUBSTITUTE(A2:A4,CHAR(10),""))+1))
D2D2=SUMPRODUCT(--(LEN(A2:A4)-LEN(SUBSTITUTE(A2:A4,CHAR(10),""))+1))
B2:B4B2=LEN(A2)-LEN(SUBSTITUTE(A2,CHAR(10),""))+1
 
Upvote 0
It looks to me like it's a case of counting all the line breaks (plus 1 for each cell) in the entire range of interest.
I think that is correct. Here is a formula to try. It may need confirmation with Ctrl+Shift+Enter in Excel 2019

23 11 19.xlsm
AB
1
2This is a line. Yet another line. How are you? What time is it?8
3More lines go here. Today is sunday. This year is 2023.
4Just one more line.
Sheet2 (3)
Cell Formulas
RangeFormula
B2B2=SUM(LEN(A2:A4)-LEN(SUBSTITUTE(A2:A4,CHAR(10),""))+1)
 
Upvote 0
@Dave Patton, @Peter_SSs

Thanks. The Sum function really has problem in this case in Excel 2019. It only yields 4 instead of 8.

As for SumProduct, it looks to be the solution for this case. I think it just needs some tweaks. I tried:

Code:
=SUMPRODUCT(--(LEN(A1:A1000)-LEN(SUBSTITUTE(A1:A1000,CHAR(10),""))+1))

It yields 1005 instead of 8, apparently counting blank cells as well. For this reason, I did some searches on the internet and found a tutorial on excluding blank cells when using SumProduct. By following its guide on using NOT(ISBLANK()), I came up with this function:

Code:
=SUMPRODUCT(--(LEN(NOT(ISBLANK(A1:A1000)))-LEN(SUBSTITUTE(NOT(ISBLANK(A1:A1000)),CHAR(10),""))+1))

However, it still incorrectly yields 1000 instead of 8. What did I do wrong?
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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