2013-05-16 52 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 
相關問題