2

因此,我制作了一个有趣且简单的宏,它随机选择 R、G 和 B 值,直到它使用所有可能的组合(跳过重复),并为每种新颜色设置 10x10 正方形的颜色值。

唯一的问题是我遇到了单元格格式数量的限制。微软说限制应该在 64000 左右,但我发现它在 Excel 2013 中的空白工作簿上正好是 65429。

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

Cells(X, Y).ClearFormats

微软列出了一些解决方案,但其中 4 种中有 3 种本质上是“不要制作太多格式”,第 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
    
4

1 回答 1

3

有几种方法可以解决这个问题,但最干净和最简单的方法是删除所有多余的样式(我见过有 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
于 2016-07-20T07:03:49.147 回答