在Excel中将2个数字与VBA循环进行比较时强制三位小数

时间:2018-04-29 20:20:01

标签: excel vba

我正在运行一个嵌套循环来比较B列中是否有7个特殊数字都高于平均(D列)控制限制。问题是当我将实际结果与三位小数与两位小数的平均值进行比较时,它会将结果四舍五入到两位小数。我将两列都格式化为带有3位小数的数字。当我调试它时会打印出来。

Sub seven_above_average()

Dim i As Integer, j As Integer, count As Integer
Dim lastRow As Integer
Dim result As Double, limit As Double

lastRow = Cells(Rows.count, "A").End(xlUp).Row

count = 0
For j = 12 To lastRow
    For i = j - 7 To j
        result = Cells(i, 2).Value
        limit = Cells(i, 4).Value
        If result >= limit Then
            count = count + 1
        End If
    Next i

    If count >= 7 Then
        For i = j To j - 7 Step -1
            Cells(i, 26).Value = 1
            Debug.Print ("Result = " & result)
            Debug.Print ("Limit = " & limit)
        Next i
    End If
    count = 0
Next j
End Sub

即使有1.389和1.388的结果,图片中的所有数据都会在第二个If条件中传递为true。

If count >= 7 then...


Picture of some data that are true for the second if

1 个答案:

答案 0 :(得分:1)

扫描您必须使用的最后7个单元格:

For i = j - 6 To j

而不是:

For i = j - 7 To j