2017-02-28 68 views
0

我有一個VBA來計算COLORED CELLS的數量。 VBA模塊與單元格對齊。但是,該功能只在我點擊單元格功能並按下ENTER時運行。更改單元格值不會自動運行該功能。 公式的自動更新也在選項中啓用。更新值更改後的單元格函數

這裏是我的VBA:

Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean) 
 
Dim rCell As Range 
 
Dim lCol As Long 
 
Dim vResult 
 
lCol = rColor.Interior.ColorIndex 
 
If SUM = True Then 
 
For Each rCell In rRange 
 
If rCell.Interior.ColorIndex = lCol Then 
 
vResult = WorksheetFunction.SUM(rCell, vResult) 
 
End If 
 
Next rCell 
 
Else 
 
For Each rCell In rRange 
 
If rCell.Interior.ColorIndex = lCol Then 
 
vResult = 1 + vResult 
 
End If 
 
Next rCell 
 
End If 
 
ColorFunction = vResult 
 
End Function

和Im使用工作表命令調用此模塊:= ColorFunction的(J70,$ B $ 3:$ BV $ 66)

任何幫助?? Thanx

回答

-1

我認爲你需要設置Application.Volatile。這裏

進一步瞭解詳細:

Refresh Excel VBA Function Results

希望它能幫助。

+0

感謝您的答覆... Application.Valatile只能當值改變了。 :(不是當背景單元格顏色變化..任何修補程序? 我不介意是否有一個按鈕來更新單元格.. –

+0

你試過這些: Ctrl + Alt + F9重新計算所有打開的工作簿中的所有工作表(完全重新計算) Shift + Ctrl + Alt + F9重建依賴關係樹並進行完整的重新計算 –

+0

Downvoter謹慎解釋? –

0

,你可以在相關的表代碼窗格的地方用一點點的解決方法

下面的代碼

Option Explicit 

Dim myColor As Long '<--| variable to store the "preceeding" color 

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    Dim coloredRng As Range 

    If myColor > 0 Then '<--| if a "colored" cell had been selected in the "preceeding" selection, then store its color 
     Me.Calculate '<--| trigger calculation and, by its effects, all functions with 'Application.Volatile' 
     myColor = 0 '<--| reset "preceeding" color to zero. it'll be set to a vaild color if user has currently selected a "colored" cell 
    End If 

    Set coloredRng = Range("J70:J73") '<--| set the range whose color change must "trigger" 'ColorFunction()' (change "J70:J73" to your actual "colored" cells addresses) 
    If Not Intersect(Target, coloredRng) Is Nothing Then myColor = Target.Interior.Color '<--| if current selection belongs to the "colored" range then store its color to trigger 'ColorFunction()' cells as soon as the user leaves the current selection 
End Sub 

這個用戶之後確實會觸發所有ColorFunction()功能有:

  • 更改了有效的單元的顏色(您在coloredRng中列出的單元之一)

  • 離開了改變有色格

所以你會遇到一點點延遲,但它會工作

+0

@KADAragorn,你通過了嗎? – user3598756