COUNTIF function
PRO
BASIC
The COUNTIF
function in Jspreadsheet Formulas Pro is a tool that helps you tally the number of cells within a specific range that satisfy a particular condition. This can be a numerical value, text, or even a date that you're looking for within the data. For example, you can use it to count how many times a certain product name appears in a sales list or how many employees have a specific job title. It's a useful way to extract specific insights from your data.
Documentation
Counts the number of cells in a range that meet a specified criterion.
Category
Statistical
Syntax
COUNTIF(range, criteria)
Parameter | Description |
---|---|
range |
The range of cells to be evaluated by the criteria. |
criteria |
The criteria used to determine which cells to count. |
Behavior
The COUNTIF
function is used to count the number of cells that meet a single criterion that you specify. The criterion can be a number, expression, text, or function that defines which cells will be counted. Here's how it generally behaves:
- Empty cells:
COUNTIF
ignores empty cells unless the criteria explicitly targets blanks using "" or "=". - Text:
COUNTIF
can count cells that contain specific text or string patterns. It is case-insensitive. - Booleans:
COUNTIF
can count cells containing Boolean values (TRUE
orFALSE
) when the criterion matches these values. - Errors: If a cell contains an error,
COUNTIF
generally does not count it unless the criterion is to specifically count cells with errors. - Wildcards:
COUNTIF
supports*
(any sequence of characters) and?
(any single character). Use them in criteria like"*text"
or"A?C"
.
Common Errors
Error | Description |
---|---|
#VALUE! | Occurs if the supplied criteria is a text string which is more than 255 characters in length. |
#N/A | Occurs if the specified range does not exist. |
Best practices
- Always check your criteria: The criteria is the heart of the
COUNTIF
function. Make sure it's set up correctly to get the desired result.- Use wildcards wisely: Wildcards can be very helpful when dealing with text, but remember that
?
matches any single character and*
matches any sequence of characters.- Be cautious with non-numeric criteria: If your criteria is a text or expression, make sure to enclose it in quotation marks.
- Handle errors: Consider using functions like
ISERROR
orIFERROR
alongsideCOUNTIF
to handle cells with errors, to prevent them from negatively impacting your count.
Usage
A few examples using the COUNTIF function.
COUNTIF(A1:A10, ">5") // Counts how many values are greater than 5
COUNTIF(B2:D5, "=Bob") // Counts how many cells exactly equal "Bob"
COUNTIF(H1:H100, "<>0") // Counts all cells that are not equal to 0
COUNTIF(A1:A10, "") // Counts blank cells in the range
COUNTIF(C1:C10, "*sales*") // Counts cells containing the word "sales" (case-insensitive)
COUNTIF(D1:D10, ">=100") // Counts values greater than or equal to 100
Interactive Spreadsheet Demo
<html>
<script src="https://jspreadsheet.com/v11/jspreadsheet.js"></script>
<script src="https://jsuites.net/v5/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v5/jsuites.css" type="text/css" />
<link rel="stylesheet" href="https://jspreadsheet.com/v11/jspreadsheet.css" type="text/css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />
<script src="https://cdn.jsdelivr.net/npm/@jspreadsheet/formula-pro/dist/index.min.js"></script>
<div id="spreadsheet"></div>
<script>
// Set your JSS license key (The following key only works for one day)
jspreadsheet.setLicense('NmQ4NGUwN2IyNDlhZGNhNWZlY2JiMmUyMjY5MjQwN2FhMTY1Yzg4ZWFiNTEyMGYxZWI4NDhhZjY0ZDAxZTk5ZDIyODExMjI2MGFlNjhiMjUzYWQ5MGZlOGFkOTNkNTU2ZjQxNjY3NWQyYTU0MWE1NTA4NjNlM2U1Mzg5YmYyMmUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnpVMk56a3pNRGszTENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2lkMlZpSWl3aWJHOWpZV3hvYjNOMElsMHNJbkJzWVc0aU9pSXpOQ0lzSW5OamIzQmxJanBiSW5ZM0lpd2lkamdpTENKMk9TSXNJbll4TUNJc0luWXhNU0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owWlhJaUxDSmlZWElpTENKMllXeHBaR0YwYVc5dWN5SXNJbk5sWVhKamFDSXNJbkJ5YVc1MElpd2ljMmhsWlhSeklpd2lZMnhwWlc1MElpd2ljMlZ5ZG1WeUlpd2ljMmhoY0dWeklpd2labTl5YldGMElsMHNJbVJsYlc4aU9uUnlkV1Y5');
// Set the extensions
jspreadsheet.setExtensions({ formula });
// Create a new spreadsheet
jspreadsheet(document.getElementById('spreadsheet'), {
worksheets: [{
data: [
[
"Product",
"Quantity",
"Count >10"
],
[
"Apples",
15,
"=COUNTIF(B:B,\">10\")"
],
[
"Bananas",
8
],
[
"Oranges",
12
],
[
"Grapes",
5
]
]
}]
});
</script>
</html>
import React, { useRef } from "react";
import { Spreadsheet, Worksheet, jspreadsheet } from "@jspreadsheet/react";
import formula from "@jspreadsheet/formula-pro";
import "jsuites/dist/jsuites.css";
import "jspreadsheet/dist/jspreadsheet.css";
// Set license
jspreadsheet.setLicense('NmQ4NGUwN2IyNDlhZGNhNWZlY2JiMmUyMjY5MjQwN2FhMTY1Yzg4ZWFiNTEyMGYxZWI4NDhhZjY0ZDAxZTk5ZDIyODExMjI2MGFlNjhiMjUzYWQ5MGZlOGFkOTNkNTU2ZjQxNjY3NWQyYTU0MWE1NTA4NjNlM2U1Mzg5YmYyMmUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnpVMk56a3pNRGszTENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2lkMlZpSWl3aWJHOWpZV3hvYjNOMElsMHNJbkJzWVc0aU9pSXpOQ0lzSW5OamIzQmxJanBiSW5ZM0lpd2lkamdpTENKMk9TSXNJbll4TUNJc0luWXhNU0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owWlhJaUxDSmlZWElpTENKMllXeHBaR0YwYVc5dWN5SXNJbk5sWVhKamFDSXNJbkJ5YVc1MElpd2ljMmhsWlhSeklpd2lZMnhwWlc1MElpd2ljMlZ5ZG1WeUlpd2ljMmhoY0dWeklpd2labTl5YldGMElsMHNJbVJsYlc4aU9uUnlkV1Y5');
// Set the extensions
jspreadsheet.setExtensions({ formula });
export default function App() {
// Spreadsheet array of worksheets
const spreadsheet = useRef();
// Worksheet data
const data = [
[
"Product",
"Quantity",
"Count >10"
],
[
"Apples",
15,
"=COUNTIF(B:B,\">10\")"
],
[
"Bananas",
8
],
[
"Oranges",
12
],
[
"Grapes",
5
]
];
// Render component
return (
<Spreadsheet ref={spreadsheet}>
<Worksheet data={data} />
</Spreadsheet>
);
}
<template>
<Spreadsheet ref="spreadsheet">
<Worksheet :data="data" />
</Spreadsheet>
</template>
<script>
import { Spreadsheet, Worksheet, jspreadsheet } from "@jspreadsheet/vue";
import "jsuites/dist/jsuites.css";
import "jspreadsheet/dist/jspreadsheet.css";
import formula from "@jspreadsheet/formula-pro";
// Set license
jspreadsheet.setLicense('NmQ4NGUwN2IyNDlhZGNhNWZlY2JiMmUyMjY5MjQwN2FhMTY1Yzg4ZWFiNTEyMGYxZWI4NDhhZjY0ZDAxZTk5ZDIyODExMjI2MGFlNjhiMjUzYWQ5MGZlOGFkOTNkNTU2ZjQxNjY3NWQyYTU0MWE1NTA4NjNlM2U1Mzg5YmYyMmUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnpVMk56a3pNRGszTENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2lkMlZpSWl3aWJHOWpZV3hvYjNOMElsMHNJbkJzWVc0aU9pSXpOQ0lzSW5OamIzQmxJanBiSW5ZM0lpd2lkamdpTENKMk9TSXNJbll4TUNJc0luWXhNU0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owWlhJaUxDSmlZWElpTENKMllXeHBaR0YwYVc5dWN5SXNJbk5sWVhKamFDSXNJbkJ5YVc1MElpd2ljMmhsWlhSeklpd2lZMnhwWlc1MElpd2ljMlZ5ZG1WeUlpd2ljMmhoY0dWeklpd2labTl5YldGMElsMHNJbVJsYlc4aU9uUnlkV1Y5');
// Set the extensions
jspreadsheet.setExtensions({ formula });
export default {
components: {
Spreadsheet,
Worksheet,
},
data() {
// Worksheet data
const data = [
[
"Product",
"Quantity",
"Count >10"
],
[
"Apples",
15,
"=COUNTIF(B:B,\">10\")"
],
[
"Bananas",
8
],
[
"Oranges",
12
],
[
"Grapes",
5
]
]
return {
data
};
}
}
</script>
import { Component, ViewChild, ElementRef } from "@angular/core";
import jspreadsheet from "jspreadsheet";
import * as formula from "@jspreadsheet/formula-pro";
// Set your JSS license key (The following key only works for one day)
jspreadsheet.setLicense('NmQ4NGUwN2IyNDlhZGNhNWZlY2JiMmUyMjY5MjQwN2FhMTY1Yzg4ZWFiNTEyMGYxZWI4NDhhZjY0ZDAxZTk5ZDIyODExMjI2MGFlNjhiMjUzYWQ5MGZlOGFkOTNkNTU2ZjQxNjY3NWQyYTU0MWE1NTA4NjNlM2U1Mzg5YmYyMmUsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnpVMk56a3pNRGszTENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2lkMlZpSWl3aWJHOWpZV3hvYjNOMElsMHNJbkJzWVc0aU9pSXpOQ0lzSW5OamIzQmxJanBiSW5ZM0lpd2lkamdpTENKMk9TSXNJbll4TUNJc0luWXhNU0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owWlhJaUxDSmlZWElpTENKMllXeHBaR0YwYVc5dWN5SXNJbk5sWVhKamFDSXNJbkJ5YVc1MElpd2ljMmhsWlhSeklpd2lZMnhwWlc1MElpd2ljMlZ5ZG1WeUlpd2ljMmhoY0dWeklpd2labTl5YldGMElsMHNJbVJsYlc4aU9uUnlkV1Y5');
// Set the extensions
jspreadsheet.setExtensions({ formula });
@Component({
standalone: true,
selector: "app-root",
template: `<div #spreadsheet></div>`
})
export class AppComponent {
@ViewChild("spreadsheet") spreadsheet: ElementRef;
// Worksheets
worksheets: jspreadsheet.worksheetInstance[];
// Create a new data grid
ngAfterViewInit() {
// Create spreadsheet
this.worksheets = jspreadsheet(this.spreadsheet.nativeElement, {
worksheets: [{
data: [
[
"Product",
"Quantity",
"Count >10"
],
[
"Apples",
15,
"=COUNTIF(B:B,\">10\")"
],
[
"Bananas",
8
],
[
"Oranges",
12
],
[
"Grapes",
5
]
]
}]
});
}
}