Excel VBA Code to count the number of cells containing cells with more than 5 open brackets

17 Views Asked by At

Need a code in Excel VBA that would go through all the sheets in a workbook, and check each cell with a value to see if it contains more than 5 open brackets "(((((". If true, the code would count the cell, and go through the same process for all the other cells to return a total count.

Example cell 1-5 contain the below value : "(test(test(test(test(test(test"

the total count returned would be 5 for this entire workbook.

Below is the current code i have but it only check one sheet

Sub CountCellsWithOpenBrackets()
    Dim ws As Worksheet
    Dim cell As Range
    Dim count As Long
    Dim i As Long
    
    ' Set the worksheet you want to work with
    Set ws = ThisWorkbook.Worksheets("Sheet1") ' Change "Sheet1" to your sheet's name

    ' Initialize the count
    count = 0

    ' Loop through each cell in the worksheet
    For Each cell In ws.UsedRange
        If Not IsEmpty(cell.Value) Then
            ' Count the number of open brackets in the cell's value
            For i = 1 To Len(cell.Value)
                If Mid(cell.Value, i, 1) = "(" Then
                    count = count + 1
                End If
            Next i

            ' Check if the count is greater than 5
            If count > 5 Then
                ' Increment the total count
                count = count + 1
            End If

            ' Reset the count for the next cell
            count = 0
        End If
    Next cell

    ' Display the total count
    MsgBox "Total cells with more than 5 open brackets: " & count
End Sub
0

There are 0 best solutions below