我的电子表格有很多条件格式,这是必要的,在第一次得到正确的颜色,但之后只是导致电子表格是缓慢和笨重,因为它检查条件格式时,任何更新。
我尝试使用以下代码替换格式,然后删除条件格式:
Set myRange = mySheet.Range("A1:CJ4000")
myRange.Interior.Color = myRange.DisplayFormat.Interior.Color
myRange.Font.Color = myRange.DisplayFormat.Font.Color
myRange.FormatConditions.Delete
它适用于有格式的单元格(保持背景颜色和字体颜色)。但是对于其他单元格,由于某种原因,它将背景颜色设置为黑色(即使它们没有填充)
我四处挖掘,发现我可以用这个来做:
For Each cell In myRange.Cells
cell.Interior.Color = cell.DisplayFormat.Interior.Color
cell.Font.Color = cell.DisplayFormat.Font.Color
Next cell
而不是第一个代码中的常规myRange更新。然而,由于有近一百万个数据单元格,我不确定这需要多长时间才能运行。
这种东西是不是不能一次性应用到很大的范围?我别无选择只能一次遍历所有单元格?
1条答案
按热度按时间ezykj2lf1#
请尝试下一个代码。它使用了两个字典来放置每种颜色(内部或字体)的
Union
范围。由于在特定限制后增加这样一个Union
范围内的单元格数量,代码可能会大大降低速度,我添加了一个序列,当它达到500个单元格的数量时,改变现有的这种范围颜色(这不是能够处理的最大值,一种通过增加它来降低速度以改变单元格颜色的最佳选择):我试着注解可能无法理解的每一行代码。如果还有什么不清楚的,请不要犹豫,要求澄清。
我在一个测试范围(“A2:CJ4001”)上测试了代码,处理大约花了112秒。
现在,我忍不住要说,我希望你两周后不要再回来了......
代码需要引用
Microsoft Scripting Runtime
。代码可以很容易地更改为使用后期绑定,但对于如此巨大的范围,您将在速度上有所放松。如果很难做到这一点,我可以发布一段代码来自动做到这一点。事实上,没有人知道你什么时候会回到这里,所以,请在运行上面的主代码**之前复制下一个代码并运行它。最好在那之后保存工作簿以保留引用。我认为这对未来非常有帮助: