I think the formula should be:
I am not sure what I did on that first attempt.. of I didn't notice a glitch...
The formula starts by looking to see IF(B3="Grand Total"
if it does then it sums according to SUMIF(B$2:B2,"*Total",J$2:J2) which means sum all values in column J from the first row to the row above the current row where column B ends with the word "Total".
If it doesn't, then it goes to next step IF(ISNUMBER(SEARCH("total",B3)) and looks to see if the column B cell in current row contains "total"... if it does then SUM(INDEX($J$2:J2,MATCH(2,1/(B$2:B2<>""))):J2). This part sums all values in column J from row where the last non-blank cell occurs in column J above current row to the cell in column J above current row. This part MATCH(2,1/(B$2:B2<>"")) finds the last non blank row in column B from B2 to row above current row....
If column B doesn't contain "total", then it goes to this part: LEN(SUBSTITUTE(D3," ","")) which was your original formula to calculate number of characters in the string with no spaces....
I hope that helps and apologize if the original formula confused you...
Bookmarks