如何在Access中为下面显示的代码创建VBA循环?

时间:2017-06-23 21:01:55

标签: excel-vba vba excel

Dim Q As Integer, F As Integer, V As Integer, hours As Integer, hours1 As Integer, hours2 As Integer, hours3 As Integer, hours4 As Integer, hours5 As Integer, hours6 As Integer, 


Q = Forms!frmweekhrs!txtWeeks * t
F = 0.9 * Q
V = 1.1 * Q
hours = Sheet.Range("b4").Value
hours1 = Sheet.Range("b5").Value
hours2 = Sheet.Range("b6").Value
hours3 = Sheet.Range("b7").Value
hours4 = Sheet.Range("b8").Value
hours5 = Sheet.Range("b9").Value
hours6 = Sheet.Range("b10").Value


If hours1 > F And hours1 < V Then
Sheet.Range("b5").Interior.ColorIndex = 4
Else
Sheet.Range("b5").Interior.ColorIndex = 3
End If

If hours2 > F And hours2 < V Then
Sheet.Range("b6").Interior.ColorIndex = 4
Else
Sheet.Range("b6").Interior.ColorIndex = 3
End If

If hours3 > F And hours3 < V Then
Sheet.Range("b7").Interior.ColorIndex = 4
Else
Sheet.Range("b7").Interior.ColorIndex = 3
End If

If hours4 > F And hours4 < V Then
Sheet.Range("b8").Interior.ColorIndex = 4
Else
Sheet.Range("b8").Interior.ColorIndex = 3
End If`

If hours5 > F And hours5 < V Then
Sheet.Range("b9").Interior.ColorIndex = 4
Else
Sheet.Range("b9").Interior.ColorIndex = 3
End If

If hours6 > F And hours6 < V Then
Sheet.Range("b10").Interior.ColorIndex = 4
Else
Sheet.Range("b10").Interior.ColorIndex = 3
End If

此代码会扫描人员小时,并将数量标记为绿色或红色,具体取决于数字是好还是坏。我还有大约40多个,并且不知道如何为此创建循环,如果您需要任何信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

Dim Q As Integer, F As Integer, V As Integer, hours As Integer

Q = Forms!frmweekhrs!txtWeeks * t
F = 0.9 * Q
V = 1.1 * Q
'initiate loop over focal rows
For i = 4 To ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
    hours = ActiveSheet.Range("b" & i).Value

    If hours > F And hours < V Then
        ActiveSheet.Range("b" & i).Interior.ColorIndex = 4
    Else
        ActiveSheet.Range("b" & i).Interior.ColorIndex = 3
    End If
Next i