Excel VBA:“太多不同的单元格格式” - 有没有办法在宏中删除或清除这些格式?

时间:2016-07-18 23:59:36

标签: excel vba excel-vba cell-formatting

所以,我做了一个有趣而简单的宏,它随机选择R,G和B值,直到它使用所有可能的组合(跳过重复),并用每种新颜色设置10x10平方的颜色值。

唯一的问题是我遇到了单元格格式数量的限制。 Microsoft says that the limit should be around 64000,但我在Excel 2013的空白工作簿中发现它正好是65429。

我已经包含了清晰的格式代码,但似乎没有效果:

Cells(X, Y).ClearFormats

Microsoft列出了一些分辨率,但其中4个基本上是“不要制作太多格式”,第4种格式是使用第三方应用程序。

在VBA中真的没有什么可以做的吗?

  • A1:J10将打印新颜色
  • K1将打印完成百分比
  • L1将打印使用的颜色数
  • M1将打印重复颜色组合的次数

    Dim CA(255, 255, 255) As Integer
    Dim CC As Long
    Dim RC As Long
    Dim R As Integer
    Dim G As Integer
    Dim B As Integer
    Dim X As Integer
    Dim Y As Integer
    
    CC = 0
    RC = 0
    
    X = 1
    Y = 1
    
    Do While ColorCount < 16777216
        R = ((Rnd * 256) - 0.5)
        G = ((Rnd * 256) - 0.5)
        B = ((Rnd * 256) - 0.5)
    
        If CA(R, G, B) <> 1 Then
            CA(R, G, B) = 1
    
            'Step down to the next row
            'If at the 10th row, jump back to the first and move to the next column
            If X < 10 Then
                X = X + 1
            Else
                X = 1
                If Y < 10 Then
                    Y = Y + 1
                Else
                    Y = 1
                End If
            End If
    
            Cells(X, Y).ClearFormats 'doesn't do what I hope :(
            Cells(X, Y).Interior.Color = RGB(R, G, B)
            CC = CC + 1
            Cells(1, 11).Value = (CC / 16777216) * 100
            Cells(1, 12).Value = CC
        Else
            RC = RC + 1
            Cells(1, 13).Value = RC
        End If
    
    Loop
    

1 个答案:

答案 0 :(得分:2)

有几种方法可以解决这个问题,但最干净,最简单的方法是删除所有额外的样式(我见过9000多种样式的工作簿)

使用以下简单的VBA代码,您可以删除所有非内置样式,并且在绝大多数情况下,这可以修复错误。

Sub removeStyles() 
Dim li as long 
On Error Resume Next 

With ActiveWorkbook 
For li = .Styles.Count To 1 Step -1 
If Not .Styles(li).BuiltIn Then 
.Styles(li).Delete 
End If 
Next 
End With 
End Sub