2012-03-12 4 views
0

私はこれを短く甘くするためにできることをします。私はそれが目標に達するまで細胞を合計する必要があるExcelワークシートを持っています。その後、これらのセルには内部カラーセットが必要です。計算は次にリセットされ、セルの次の実行がターゲットに等しくなるまで範囲を継続します。このセットのインテリアは異なる色に設定する必要があります。これは範囲を交互に繰り返すでしょう。私はそれを約90%持っていますが、2番目の色はセットの開始点のみを強調表示し、最初の色で続きます。私が持っている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 
+0

私はインデントを読みやすくするために変更しました。これがあなたが意味するものを確認できますか? – assylias

答えて

1

これは、あなたが(それが目標に到達するたびに色を交互にしてあれば、前方のバランスを運ぶ)必要なものかもしれないです:

Sub calctarget() 
    Dim c As Range 
    Dim target As Double 
    Dim runTotal As Double 
    Dim currentColor As Long 

    target = 44500 
    currentColor = 4 

    For Each c In Range("a1:a15") 
     If IsNumeric(c) Then 
      runTotal = runTotal + c 
     End If 
     If runTotal >= target Then 'Target reached 
      currentColor = IIf(currentColor = 4, 5, 4) 'alternate colors (4 and 5) 
      runTotal = runTotal - target 'maybe you want to start from 0 again? 
     End If 
     c.Interior.ColorIndex = currentColor 
    Next c 
End Sub 
+0

それは私に一歩近づく涼しい。しかし計算はちょっとだけです。 2番目のセットは、> targetを実行します。これは、本質的に、製品重量のセットからトラック負荷を構築することになります。セルは連続したスケジュールに従うために連続して合計しなければならない。だから、細胞が一度目標を達成することができるように細胞を一旦近づけたら、それは一つの負荷になります。次のセットは、ターゲットに近づくまで再び0で開始します。リンス/リピート..これは私に非常に近い計算を把握させてくれます。ありがとう – PCGIZMO

+0

@PCGIZMO 'runTotal = runTotal - target'を' runTotal = 0'に変更するだけです。 – assylias

+0

{https:// docs。 google.com/open?id=0BzGnV1BGYQbvMERWU3VkdGFTQS1tYXpXcU1Mc3lmUQ} – PCGIZMO