2013-05-16 51 views
3

嗨即时新的使用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 
+0

你想检查哪个范围?您的代码不检查任何单元格值。另外,这个_might_可以通过'= COUNTIF'来实现。 – Passerby

回答

3

为什么使用VBA?

使用COUNTIFS()函数。只需在单元格B13中输入它,并将范围从A:A调整到您需要检查公式的任何范围。

=COUNTIFS(A:A, ">=500", A:A, "<=750") 
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 
相关问题