How can I use VBA to count the number of cells with specific text? 2

How can I use VBA to count the number of cells with specific text?

VBA, or Visual Basic for Applications, is a programming language that can be used in Microsoft Excel to automate tasks and perform complex operations. Using VBA, it is possible to count the number of cells in a given range that contain a specific text or string. This can be achieved by writing a simple code that loops through the cells and uses conditional statements to identify and count the cells with the specified text. This feature can be useful for data analysis and allows for efficient and accurate counting of cells with specific text in a large dataset.

VBA: Count Cells with Specific Text


You can use the following basic syntax to count the number of cells in a range that contain a specific text using VBA:

Sub CountCellsWithText()
    Range("D2") = WorksheetFunction.CountIf(Range("A2:A13"), "*avs*")
End Sub

This particular example counts the number of cells in the range A2:A13 that contain “avs” and then assigns the result to cell D2.

Note: The asterisks ( * ) are used as wildcard characters in the Countif function.

If you would instead like to display the count of cells in a message box, you can use the following syntax:

Sub CountCellsWithText()

    Dim cellCount As Integer
    
    'Calculate number of cells that contain 'avs'
    cellCount = WorksheetFunction.CountIf(Range("A2:A13"), "*avs*")
    
    'Display the result
    MsgBox "Cells that contain avs: " & cellCountEnd Sub

The following examples shows how to use each of these methods in practice with the following dataset in Excel that contains information about various basketball players:

Example 1: Count Cells with Specific Text Using VBA and Display Results in Cell

Suppose we would like to count the number of cells that contain “avs” in the team name and output the results in a specific cell.

We can create the following macro to do so:

Sub CountCellsWithText()
    Range("D2") = WorksheetFunction.CountIf(Range("A2:A13"), "*avs*")
End Sub

When we run this macro, we receive the following output:

Notice that cell D2 contains a value of 5.

This tells us that the there are 5 cells in the range A2:A13 that contain “avs” in the team name.

Example 2: Count Cells with Specific Text Using VBA and Display Results in Message Box

We can create the following macro to do so:

Sub CountCellsWithText()

    Dim cellCount As Integer
    
    'Calculate number of cells that contain 'avs'
    cellCount = WorksheetFunction.CountIf(Range("A2:A13"), "*avs*")
    
    'Display the result
    MsgBox "Cells that contain avs: " & cellCountEnd Sub

When we run this macro, we receive the following output:

The message box tells us that there are 5 cells that contain “avs” in the team name.

Cite this article

stats writer (2024). How can I use VBA to count the number of cells with specific text?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-count-the-number-of-cells-with-specific-text/

stats writer. "How can I use VBA to count the number of cells with specific text?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-count-the-number-of-cells-with-specific-text/.

stats writer. "How can I use VBA to count the number of cells with specific text?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-count-the-number-of-cells-with-specific-text/.

stats writer (2024) 'How can I use VBA to count the number of cells with specific text?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-count-the-number-of-cells-with-specific-text/.

[1] stats writer, "How can I use VBA to count the number of cells with specific text?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use VBA to count the number of cells with specific text?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top