太多不同的单元格格式

Cra*_*aig 17 excel vba excel-2003 excel-vba

  1. 我有一个大型文件,从头开始重新创建10张,更新了12张,原始数据加载了5张,报告的宏使用了7张.
  2. 我最近添加了一个新工作表,并且我遇到了Excel "太多不同的单元格格式"问题.

我试过的

我已经阅读了一些谷歌搜索结果,他们说我应该简化格式化,但我甚至不知道我如何获得4000种不同的单元格格式,更不用说我使用了哪些格式,所以我可以删除一些.

它也会弹出一些文件运行的时间,但不是全部,直到它出现,然后每次运行时都会发生.由于宏正在做很多工作,包括从头开始创建10张,我不知道该怎么做.

有人知道吗

  • 我可以运行一个宏来获取所有单元格格式的列表以及有多少单元格正在使用它们?
  • 他们信任的程序,以帮助删除多余的单元格格式?

谢谢

Chu*_*erd 46

您描述的问题导致我(和同事)在使用Excel 2010时失去了许多小时的工作效率.以下VBA代码/宏帮助我将.xlsm文件从使用3540样式降低到34.

' Description:
'    Borrowed largely from http://www.jkp-ads.com/Articles/styles06.asp

Option Explicit

' Description:
'    This is the "driver" for the entire module.
Public Sub DropUnusedStyles()

    Dim styleObj As Style
    Dim rngCell As Range
    Dim wb As Workbook
    Dim wsh As Worksheet
    Dim str As String
    Dim iStyleCount As Long
    Dim dict As New Scripting.Dictionary    ' <- from Tools / References... / "Microsoft Scripting Runtime"

    ' wb := workbook of interest.  Choose one of the following
    ' Set wb = ThisWorkbook ' choose this module's workbook
    Set wb = ActiveWorkbook ' the active workbook in excel


    Debug.Print "BEGINNING # of styles in workbook: " & wb.Styles.Count
    MsgBox "BEGINNING # of styles in workbook: " & wb.Styles.Count

    ' dict := list of styles
    For Each styleObj In wb.Styles
        str = styleObj.NameLocal
        iStyleCount = iStyleCount + 1
        Call dict.Add(str, 0)    ' First time:  adds keys
    Next styleObj
    Debug.Print "  dictionary now has " & dict.Count & " entries."
    ' Status, dictionary has styles (key) which are known to workbook


    ' Traverse each visible worksheet and increment count each style occurrence
    For Each wsh In wb.Worksheets
        If wsh.Visible Then
            For Each rngCell In wsh.UsedRange.Cells
                str = rngCell.Style
                dict.Item(str) = dict.Item(str) + 1     ' This time:  counts occurrences
            Next rngCell
        End If
    Next wsh
    ' Status, dictionary styles (key) has cell occurrence count (item)


    ' Try to delete unused styles
    Dim aKey As Variant
    On Error Resume Next    ' wb.Styles(aKey).Delete may throw error

    For Each aKey In dict.Keys

        ' display count & stylename
        '    e.g. "24   Normal"
        Debug.Print dict.Item(aKey) & vbTab & aKey

        If dict.Item(aKey) = 0 Then
            ' Occurrence count (Item) indicates this style is not used
            Call wb.Styles(aKey).Delete
            If Err.Number <> 0 Then
                Debug.Print vbTab & "^-- failed to delete"
                Err.Clear
            End If
            Call dict.Remove(aKey)
        End If

    Next aKey

    Debug.Print "ENDING # of style in workbook: " & wb.Styles.Count
    MsgBox "ENDING # of style in workbook: " & wb.Styles.Count

End Sub
Run Code Online (Sandbox Code Playgroud)

  • 请注意,定义iStyleeCount As Integer可能会产生溢出 (3认同)
  • 将ActiveWorkbook简化为活动样式后,您可能希望使用基本样式重新填充.实现此目的的一种方法是创建新工作簿...然后通过键盘快捷键ALT-HJM将新工作簿样式合并到清理区域. (3认同)
  • +1.在最后添加东西以释放内存和定时器t为单:"Set styleObj = Nothing","Set rngCell = Nothing","Set wb = Nothing","Set wsh = Nothing","Set dict = Nothing", `Debug.Print Timer - t&"secondes."`. (3认同)
  • 当谷歌搜索"excel太多格式"时,这需要成为最佳结果.MS"帮助"完全没有帮助.这对我有用,并删除了几千种格式,直到9.这是一个很大的PLUS ONE +1 (3认同)
  • 这对我来说非常好.修复了两个几乎不可能编辑的关键40MB +工作簿.非常感谢! (2认同)

Mik*_*wis 7

"细胞格式"很复杂.单元格实际上没有"格式".它们有一个字体(它本身有一个名称和大小),一个NumberFormat,Height,Width,Orientation等.

所以你需要用"格式"来定义你的意思.

下面是获取字体名称和大小的代码.您可以替换您喜欢的任何属性.

下面的代码假定您已在工作簿中创建名为"格式"的工作表.运行宏后,字体名称和大小将列在该工作表中.

Public Sub GetFormats()

    Dim CurrentSheet As Integer
    Dim UsedRange As Range
    Dim CurrentCell As Range
    Dim rw As Long

    Sheets("Formats").Cells.ClearContents
    rw = 1
    For CurrentSheet = 1 To Sheets.Count
        Set UsedRange = Range(Sheets(CurrentSheet).Range("A1"), Sheets(CurrentSheet).Range("A1").SpecialCells(xlLastCell))
        For Each CurrentCell In UsedRange
            FontUsed = CurrentCell.Font.Name + ":" + CStr(CurrentCell.Font.Size)
            If Sheets("Formats").Cells.Find(FontUsed) Is Nothing Then
                Sheets("Formats").Cells(rw, 1).Value = FontUsed
                rw = rw + 1
            End If
        Next
    Next CurrentSheet
End Sub
Run Code Online (Sandbox Code Playgroud)


小智 6

很多人似乎都遇到了这个问题.

大多数情况下,问题与​​过多的未使用且经常损坏的样式有关 ,而与单元格唯一单元格格式组合的总数相关.

我写了一个实用程序来修复XL2007 OOXML文件,可以保存到XL2003.这是博客文章的链接:

  • 需要.Net3.5和MS Excel 2007.
  • 将修复xlsx或xlsm文件.
  • 该帖子有一个随附应用程序的自述文件.

不需要像在其他论坛上建议的那样使用Open Office来冒险进一步破坏您的文件