2017-02-28 41 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

相关问题