在更改单元格背景时重新计算

Posted

技术标签:

【中文标题】在更改单元格背景时重新计算【英文标题】:Recalculate on changing cell background 【发布时间】:2016-06-03 16:21:56 【问题描述】:

我有一个 UDF 与 IF 语句相结合,在具有背景颜色的单元格上放置一个“X”,当我更改背景颜色时,公式不会重新计算我如何使用 Private Sub Workbook_SheetChange 以便每次我更改单元格背景颜色所有公式都会重新计算。

公式很简单=IF(CELLCOLOR(M2)="Custom color or no fill","","x"

这里是 UDF

Function CELLCOLOR(rCell As Range, Optional ColorName As Boolean)
Application.Volatile
Dim strColor As String
Dim iIndexNum As Integer

Select Case rCell.Interior.ColorIndex

Case 1

strColor = "Black"

iIndexNum = 1

Case 53

strColor = "Brown"

iIndexNum = 53

Case 52

strColor = "Olive Green"

iIndexNum = 52

Case 51

strColor = "Dark Green"

iIndexNum = 51

Case 49

strColor = "Dark Teal"

iIndexNum = 49

Case 11

strColor = "Dark Blue"

iIndexNum = 11

Case 55

strColor = "Indigo"

iIndexNum = 55

Case 56

strColor = "Gray-80%"

iIndexNum = 56

Case 9

strColor = "Dark Red"

iIndexNum = 9

Case 46

strColor = "Orange"

iIndexNum = 46

Case 12

strColor = "Dark Yellow"

iIndexNum = 12

Case 10

strColor = "Green"

iIndexNum = 10

Case 14

strColor = "Teal"

iIndexNum = 14

Case 5

strColor = "Blue"

iIndexNum = 5

Case 47

strColor = "Blue-Gray"

iIndexNum = 47

Case 16

strColor = "Gray-50%"

iIndexNum = 16

Case 3

strColor = "Red"

iIndexNum = 3

Case 45

strColor = "Light Orange"

iIndexNum = 45

Case 43

strColor = "Lime"

iIndexNum = 43

Case 50

strColor = "Sea Green"

iIndexNum = 50

Case 42

strColor = "Aqua"

iIndexNum = 42

Case 41

strColor = "Light Blue"

iIndexNum = 41

Case 13

strColor = "Violet"

iIndexNum = 13

Case 48

strColor = "Gray-40%"

iIndexNum = 48

Case 7

strColor = "Pink"

iIndexNum = 7

Case 44

strColor = "Gold"

iIndexNum = 44

Case 6

strColor = "Yellow"

iIndexNum = 6

Case 4

strColor = "Bright Green"

iIndexNum = 4

Case 8

strColor = "Turqoise"

iIndexNum = 8

Case 33

strColor = "Sky Blue"

iIndexNum = 33

Case 54

strColor = "Plum"

iIndexNum = 54

Case 15

strColor = "Gray-25%"

iIndexNum = 15

Case 38

strColor = "Rose"

iIndexNum = 38

Case 40

strColor = "Tan"

iIndexNum = 40

Case 36

strColor = "Light Yellow"

iIndexNum = 36

Case 35

strColor = "Light Green"

iIndexNum = 35

Case 34

strColor = "Light Turqoise"

iIndexNum = 34

Case 37

strColor = "Pale Blue"

iIndexNum = 37

Case 39

strColor = "Lavendar"

iIndexNum = 39

Case 2

strColor = "White"

iIndexNum = 2

Case Else

strColor = "Custom color or no fill"

End Select



If ColorName = False Or strColor = "Custom color or no fill" Then

CELLCOLOR = strColor

Else

CELLCOLOR = iIndexNum

End If


'All credits go to the dude who wrote this code which I'm not
End Function

【问题讨论】:

Excel 不提供由单元格中的颜色变化触发的事件。但是,有几种解决方法的可能性。基本上,如果您的单元格范围是 A1:D50,则在 AA1:AD50 处设置一个平行范围,并为每个单元格使用您的 UDF 用字符串颜色或 indexNumber 填充这些平行单元格。然后,当您的数据更改(即Worksheet_Change)时,您的更改区域应查看该平行区域并进行相应操作。 当您选择一个单元格时,可以使用Worksheet_SelectionChange跟踪,然后再次选择选择更改后检查该范围是否已更改。或者,如果您使用的范围不是很大,只要选择更改就触发重新计算。 检查此链接 - 使用字典来捕获单元格格式状态 ***.com/questions/21342408/…。您可能需要引用 Scripting.Runtime 库来获取 Dictionary 对象。 @TimWilliams 如何触发对选择更改的重新计算...我从来没有这样做过 【参考方案1】:

此代码进入工作表代码模块(不在常规模块中)

编辑“A1:D50”以覆盖包含单元格颜色 UDF 的范围

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Me.Range("A1:D50").Calculate
End Sub

【讨论】:

如果这个实现了,把Application.Volatile从函数中去掉,否则会花费很多时间。

以上是关于在更改单元格背景时重新计算的主要内容,如果未能解决你的问题,请参考以下文章

滚动时collectionview更改单元格背景图像

JTable:单击按钮时更改单元格背景

如何在没有重载数据的情况下更改uitableview中的高度单元格

更改视图外单元格的背景颜色

重新调整尺寸时更改设计集合视图单元格

设备方向更改时如何重新排列集合视图单元格