Count if or VBA or Code

Vishaal

Well-known Member
Joined
Mar 16, 2019
Messages
543
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
  2. Web
Hi All,

Thanks in advance for your help.

we have the following sheet, its a sample sheet (have data approx 90 row and 100 column)

Excel 2010 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]
Marks​
[/td][td]
Ram​
[/td][td]
Ravi​
[/td][td]
Rozi​
[/td][td]
Sunny​
[/td][td]
Ahmed​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]
45​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]
65​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]
25​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]
35​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]
26​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]
98​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td]
78​
[/td][td]
Na​
[/td][td]
yes​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]
15​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
yes​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td]
45​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
yes​
[/td][td]
Na​
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet1[/td][/tr][/table]



Need the following result

Excel 2010 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]
Marks​
[/td][td]
Ram​
[/td][td]
Ravi​
[/td][td]
Rozi​
[/td][td]
Sunny​
[/td][td]
Ahmed​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]
45​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]
65​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]
25​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]
35​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]
26​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]
98​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td]
78​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td]
yes​
[/td][td]
Na​
[/td][td]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]
15​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td]
yes​
[/td][td]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td]
45​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td]
yes​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
11
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
12
[/td][td]
Total "Na" in continuation from bottom​
[/td][td]
4​
[/td][td]
2​
[/td][td]
1​
[/td][td]
[/td][td]
6​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
13
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
14
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet1[/td][/tr][/table]

We have counted Total "Na" which are in continuation from bottom. After this we want to compare
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Here is a UDF (user defined function) that you can use...
Code:
Function CountNA(Rng As Range) As Variant
  Dim Arr As Variant
  Arr = Split(Join(Application.Transpose(Rng), ""), "yes", , vbTextCompare)
  CountNA = Len(Arr(UBound(Arr))) / 2
  If CountNA = 0 Then CountNA = ""
End Function

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use CountNA just like it was a built-in Excel function. For example,

=CountNA(B2:B10)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
Thanks for the help Rick Rothstein Sir Ji,

i have used the formula but its not giving accurate result, check in below sheet

Excel 2010 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]
First Data​
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]
Marks​
[/td][td]
Ram​
[/td][td]
Ravi​
[/td][td]
Rozi​
[/td][td]
Sunny​
[/td][td]
Ahmed​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]
45​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]
65​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]
25​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]
35​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]
26​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td]
Yes​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td]
98​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]
78​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td]
yes​
[/td][td]
Na​
[/td][td]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td]
15​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td]
yes​
[/td][td]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
11
[/td][td]
45​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][td]
yes​
[/td][td=bgcolor:#FFFF00]
Na​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
12
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
13
[/td][td]Total "Na" in continuation from bottom[/td][td]
4​
[/td][td]
2​
[/td][td]
1​
[/td][td]
[/td][td]
6​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
14
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
15
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
16
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
17
[/td][td]with your instructions[/td][td]
3
[/td][td]
1
[/td][td]
[/td][td]
3
[/td][td]
5
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet1[/td][/tr][/table]

Help pls
 
Upvote 0
When my example showed this formula to use...

=CountNA(B2:B10)

the range was based on the data you posted in Message #1 ... the data you posted in Message #3 has everything one row lower. So the data you want to count is no longer in the range B2:B10 for the first column... you moved it to B3:B11 so the formula you must use is this...

=CountNA(B3:B10)

The formula cannot figure out on its own where your data is... you must tell it. So, if you add more rows of data, you will have to adjust the range given to the UDF so it knows where the data it has to parse is located at.
 
Last edited:
Upvote 0
Thanks for the help Rick Rothstein Sir Ji,

I am really sorry for not read carefully, really sorry

its working

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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