我为项目构建了一个带有几个公式的Excel工作表。然后,我添加了一个CommandButton,用于折叠/展开某些单元格组。
CommandButton代码为:
Private Sub CommandButton1_Click()
Columns("B").ColumnWidth = 30
With Me.CommandButton1
If .Caption = "Customer Relationship" & vbCrLf & "Management Processes" Then
.Caption = "Customer Relationship" & vbCrLf & "Management Processes "
Rows("11:74").Hidden = False
Else
.Caption = "Customer Relationship" & vbCrLf & "Management Processes"
Rows("11:74").Hidden = True
End If
'Worksheets(1).Calculate
End With
End Sub
它正常工作。现在,在工作表上,我有一个单元格,该单元格使用另一个UDF根据单元格的背景色求和,该单元格的代码是:
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
它也像一种魅力一样起作用。问题出在这里:当我按下CommandButton时,单元格正确折叠/展开,但是具有按颜色求和功能的单元格变为VALUE !,然后如果我单击公式内的公式栏并返回工作表,它会自动修复自己,而无需我进行任何更改。
我试图: a-在按钮上添加重新计算,没有骰子。 b-放弃该功能,我尝试过使用其他代码,它做同样的事情。 c-放弃该公式已变得疯狂,因为它不再“看到”折叠的单元格。它确实看到了它们,当我折叠该组并“修复”单击它的公式时,即使此时该单元格处于隐藏状态,它也可以正确求和。
有帮助吗? 预先感谢
答案 0 :(得分:0)
单击命令按钮时,至少将“功能”中提到的单元格之一设置为“脏”。这将触发重新计算。
Private Sub CommandButton1_Click()
Columns("B").ColumnWidth = 30
With Me.CommandButton1
If .Caption = "Customer Relationship" & vbCrLf & "Management Processes" Then
.Caption = "Customer Relationship" & vbCrLf & "Management Processes "
Rows("11:74").Hidden = False
Else
.Caption = "Customer Relationship" & vbCrLf & "Management Processes"
Rows("11:74").Hidden = True
End If
'Worksheets(1).Calculate
End With
Cells(1, 6).Dirty 'For Cell F1
End Sub