User Defined Function to count number cells

kayza

Board Regular
Joined
Apr 29, 2015
Messages
61
Office Version
  1. 2007
Platform
  1. Windows
Hi, I want to count the number of selected cells in a range, then convert them to a value of 0 and 1.

ABCDEFG
1Data 1Data 2Data 3Data 4Result
2AppleBanana1100
3OrangeLemon0101
4Grape0010

So far, the code I can work with is like this. but the results are not as expected

VBA Code:
Function Tot(RR As Range)
Dim cell As Range
Tot = "0"
For Each cell In RR
If cell.Value = "" Then
    Tot = Tot & 1
End If
Next cell
End Function

Thank you in advance
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
What version of Excel do you have?
Please update you account details to show this, as it affects which functions you can use.
 
Upvote 0
How about this?

Book1
ABCDEFGH
1Data 1Data 2Data 3Data 4ResultArray Formula
2AppleBanana11001100
3OrangeLemon01010101
4Grape00101011
Sheet1
Cell Formulas
RangeFormula
H2:H4H2=TEXT(DEC2BIN(SUMPRODUCT((TRANSPOSE(A2:D2)=0)*(2^(ROW(INDIRECT("1:"&COLUMNS(A2:D2)))-1)))),"0000")
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
Two other options
+Fluff v2.xlsm
ABCDEFG
1Data 1Data 2Data 3Data 4UDFFormula
2AppleBanana11001100
3OrangeLemon01010101
4Grape00100010
Work
Cell Formulas
RangeFormula
F2:F4F2=kayza(A2:D2)
G2:G4G2=TEXTJOIN(,,IF(A2:D2="",0,1))


VBA Code:
Function kayza(Rng As Range) As String
   Dim Cl As Range
   
   For Each Cl In Rng
      kayza = kayza & IIf(Cl = "", 0, 1)
   Next Cl
End Function
 
Upvote 0
Solution
I noticed the original formula I posted didn't produce correct results for each of the rows. This one seems to do it correctly.

Book1
ABCDEFGH
1Data 1Data 2Data 3Data 4ResultArray Formula
2AppleBanana11001100
3OrangeLemon01010101
4Grape00100010
5AppleBanana0110
Sheet1
Cell Formulas
RangeFormula
H2:H5H2=TEXT(DEC2BIN(SUM((INDEX(A2:D2,N(IF(1,{4;3;2;1})))<>0)*(2^(ROW(INDIRECT("1:"&COLUMNS(A2:D2)))-1)))),"0000")
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
And thinking about it, the formula can be made a lot more simple.

Book1
ABCDEFGHI
1Data 1Data 2Data 3Data 4ResultArray FormulaLess Crazy Formula
2AppleBanana110011001100
3OrangeLemon010101010101
4Grape001000100010
5AppleBanana011001100110
Sheet1
Cell Formulas
RangeFormula
H2:H5H2=TEXT(DEC2BIN(SUM((INDEX(A2:D2,N(IF(1,{4;3;2;1})))<>0)*(2^(ROW(INDIRECT("1:"&COLUMNS(A2:D2)))-1)))),"0000")
I2:I5I2=DEC2BIN(SUM((A2:D2<>"")*(2^{3,2,1,0})),4)
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
Non-array version:

Excel Formula:
=DEC2BIN(SUMPRODUCT((A2:D2<>"")*2^{3,2,1,0}),4)

or for an arbitrary number of columns:

Excel Formula:
=DEC2BIN(SUMPRODUCT((A2:D2<>"")*2^(COLUMN(D2)-COLUMN(A2:D2))),COLUMNS(A2:D2))
 
Upvote 0
Thank you for all the responses

Earlier, I apologize for not mentioning which version of Excel I am using. So that some functions did not work properly on my workbook
But I say many thanks for all the help.

and for mr Fluff. the UDF function you wrote works perfectly. Thank you very much
 
Upvote 0
Glad we could help & thanks for the feedback.

Don't forget to update your account details to show your version of Excel & then scroll down & click save.
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

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