计算包含特定值的单元格

时间:2013-05-16 02:26:41

标签: excel-vba vba excel

你好我使用excel的新东西,我想计算包含500和750之间的值的单元格。这是我写的代码,但我做错了什么,没有给我正确的答案。有人可以帮忙吗?

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

2 个答案:

答案 0 :(得分:3)

为什么要使用VBA?

使用COUNTIFS()功能。只需在单元格B13中输入,然后将范围从A:A调整到您需要检查公式的范围。

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

答案 1 :(得分:0)

您的代码几乎没有调整。

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
相关问题