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

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

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

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

Cells(X, Y).ClearFormats 

微软列出了一些解决scheme,但是其中的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 

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

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

 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