Cra*_*aig 17 excel vba excel-2003 excel-vba
我试过的
我已经阅读了一些谷歌搜索结果,他们说我应该简化格式化,但我甚至不知道我如何获得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)
"细胞格式"很复杂.单元格实际上没有"格式".它们有一个字体(它本身有一个名称和大小),一个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)
| 归档时间: |
|
| 查看次数: |
97097 次 |
| 最近记录: |