我會盡我所能來做到這一點。我有一個Excel工作表,需要對單元格進行求和,直到達到目標。然後這些單元需要一個內部顏色集。然後重置計算,然後繼續下去,直到下一輪細胞等於目標。內部應該設置爲不同的顏色。這將在整個範圍內交替。我擁有它約90%,但第二種顏色只突出了集合的開始,然後繼續第一種顏色。預先感謝您的任何指針Excel 2007 VBA Cell.interior基於連續單元格計算交替對目標
我:
Sub calctarget()
Dim x As Variant
Dim xsub As Variant
Dim total As Double
Dim y As Variant
Dim target As Double
Dim RunTotal As Double
target = 44500
If RunTotal < target Then y = 0
RunTotal = 0
For Each y In Range("a1:a15")
On Error Resume Next
RunTotal = RunTotal + y.Value
If RunTotal < target Then
y.Interior.ColorIndex = 4
End If
If RunTotal > target Then
RunTotal = RunTotal - y
If y.Offset(-1, 0).Interior.ColorIndex = 4 Then
RunTotal = 0
If RunTotal = 0 Then y.Interior.ColorIndex = 5
RunTotal = RunTotal + y
End If
End If
Next y
End Sub
我已經改變了縮進以使其更容易閱讀 - 你能檢查這是你的意思嗎? – assylias 2012-03-12 15:10:44