Home » excel » excel vba – Counting Cells containing especific Values

excel vba – Counting Cells containing especific Values

Posted by: admin April 23, 2020 Leave a comment

Questions:

Hi im new using excel and i would like to count the cell that containg the values betweeen 500 and 750. this is the code that i wrote but i did something wrong that is not giving me the right answer. Can some one please help?

Sub Count()
    Dim i As Integer
    Dim j As Integer

    Range("C3").Select

    For i = 1 To 279
        For j = 1 To 19
            If i > 500 Then
            ElseIf i <= 750 Then
                i = i + 1
            End If
        Next j
    Next i

    Sheets("Sheet1").Select
    Range("B13").Select
    ActiveCell.Value = i
End Sub
How to&Answers:

Why use VBA for that?

Use COUNTIFS() function. Just enter it in cell B13 and adjust the range from A:A to whatever range you need to check in the formula.

=COUNTIFS(A:A, ">=500", A:A, "<=750")

Answer:

Your code with few tweaks works.

Sub count()
    Dim i As Integer
    Dim j As Integer
    Dim countCell As Integer

    Range("C3").Select

    For i = 1 To 279
        For j = 1 To 19
            If Cells(i, j).Value >= 500 And Cells(i, j).Value <= 750 Then
                countCell = countCell + 1
            End If
        Next
    Next

    MsgBox countCell & " Cells Found", vbInformation

    Sheets("Sheet1").Select
    Range("B13").Select
    ActiveCell.Value = i
End Sub