Count number of values in a cell unless blank

2019-02-28 07:57发布

I have a cell that uses a drop-down list that allows for multiple selections. How do I count the number of selections?

I used this: =LEN(A2)-LEN(SUBSTITUTE(A2,",",""))+1

But this doesn't account for blank or no selection. How can I display 0 as well if there is no selection?

enter image description here

2条回答
等我变得足够好
2楼-- · 2019-02-28 08:13

If your selections do not have spaces then substitute commas for spaces, trim the result then count for spaces as above with commas.

=LEN(trim(SUBSTITUTE(A2,","," ")))-LEN(SUBSTITUTE(trim(SUBSTITUTE(A2,","," "))," ",""))+sign(len(a2))
查看更多
疯言疯语
3楼-- · 2019-02-28 08:15

If you want your formula to ignore consecutive comma's:

=LEN(A2)-LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,",,",","),",,",","),",",""))+1

It's doubled-up to account for odd numbers of sets of commas.


or to totally jack-all @Jeeped's suggestions, if the values in your drop down do not have spaces, a better way might be:

=LEN(A2)-LEN(TRIM(SUBSTITUTE(SUBSTITUTE(A2,","," ")," ","")))+1
查看更多
登录 后发表回答