网站首页 > 技术文章 正文
问题:
Excel工作簿内含有很多自定义数字格式,但实际并未使用。强迫症一犯起来,就想删光它们。可是Excel只能一个个删除,实在太费劲。
VBA能不能一键清理所有未使用的自定义数字格式?答案是肯定的。
先上效果图:
VBA程序效果:
Talk is cheap, show me the code.
VBA程序源码:
Sub deleteAllUnusedCustomNumberFormats(Optional wbk As Workbook) ' v 0.0.1 ' jeffreyjcli-a-t-qq.com
On Error Resume Next
sPrompt = "是否删除所有自定义数字格式?" & vbCrLf & "(Delete unused custom number formats from the workbook?)"
vAnsr = MsgBox(sPrompt, vbYesNoCancel + vbQuestion + vbDefaultButton1 + vbApplicationModal)
Application.ScreenUpdating = False
If vAnsr = vbCancel Or vAnsr = vbNo Then Exit Sub
If wbk Is Nothing Then Set wbk = ActiveWorkbook
Set wst = wbk.Sheets.Add
Set rng = wst.Range("A2")
rng.Select
Do
vSetFormat = rng.NumberFormatLocal
DoEvents
SendKeys "{Tab 3}{Down}{Enter}"
Application.Dialogs(xlDialogFormatNumber).Show vSetFormat
ReDim Preserve vCustomNumberFormats(0 To ll)
vCustomNumberFormats(ll) = rng.NumberFormatLocal
ll = ll + 1
Loop Until vCustomNumberFormats(ll - 1) = vSetFormat
vCustomNumberFormats = RemoveDuplicateArray(vCustomNumberFormats)
sCustomNumberFormats = Join(vCustomNumberFormats, vbCrLf)
ll = 0
For Each ws1 In wbk.Sheets
For Each cll In ws1.UsedRange.Cells
If InStr(1, sCustomNumberFormats, cll.NumberFormatLocal) >= 1 Then
ReDim Preserve vUsedCustomNumberFormats(0 To ll)
vUsedCustomNumberFormats(ll) = cll.NumberFormatLocal
ll = ll + 1
End If
Next
Next
vUsedCustomNumberFormats = RemoveDuplicateArray(vUsedCustomNumberFormats)
sUsedCustomNumberFormats = Join(vUsedCustomNumberFormats, vbCrLf)
vUnusedCustomNumberFormats = RemoveDuplicateArray(ArraySubtract(vCustomNumberFormats, vUsedCustomNumberFormats))
sUnusedCustomNumberFormats = Join(vUnusedCustomNumberFormats, vbCrLf)
If vAnsr = vbYes Then
jj = 0
For ll = LBound(vUnusedCustomNumberFormats) To UBound(vUnusedCustomNumberFormats)
wbk.DeleteNumberFormat (vUnusedCustomNumberFormats(ll))
If Err.Number = 0 Then
ReDim Preserve vDeletedUnusedCustomNumberFormats(0 To jj)
vDeletedUnusedCustomNumberFormats(jj) = vUnusedCustomNumberFormats(ll)
jj = jj + 1
Else
Err.Clear
End If
Next
Call MsgBox("完成。已删除以下未使用的自定义数字格式:" & vbCrLf & _
"Done. The following custom NumberFormats have been deleted:" & vbCrLf & vbCrLf & _
Join(vDeletedUnusedCustomNumberFormats, vbCrLf), _
vbInformation + vbYes)
End If
Application.ScreenUpdating = True
If MsgBox("删除临时工作表? " & vbCrLf & " Delete the temporary sheet?", vbYesNoCancel + vbQuestion) = vbYes Then
Application.DisplayAlerts = False
wst.Delete
Application.DisplayAlerts = True
End If
End Sub
OK. 分享完毕。
如果需要批量处理多个工作簿,或者需要插件随时在任意工作簿一键运行的话,可以私信我。
感谢您的关注、点赞、收藏与点评。下期见~
本文标题:《Excel VBA从入门到入神系列之提高篇》:Excel工作簿大瘦身:一键删除自定义数字格式
猜你喜欢
- 2024-11-19 正确卸载SQLSERVER的方法详解
- 2024-11-19 Excel VBA:一键删除所有数据有效性规则
- 2024-11-19 sd卡文件删除了怎么恢复数据?sd卡删除数据恢复教程
- 2024-11-19 电脑快捷指令误删文件怎么办?四种方法找回
- 2024-11-19 Photoshop快捷键
- 2024-11-19 使用Excel删除重复数据所在的行,多种方法教你解决不同情况
- 2024-11-19 全程软件测试(七十九):MySQL数据表删除及简单查询—读书笔记
- 2024-11-19 PS快捷键,你知道哪些?
- 2024-11-19 Photoshop 抠图有哪些方法和技巧?
- 2024-11-19 任达华这个自救动作被点赞!5种外伤紧急处理方法速看
- 标签列表
-
- content-disposition (47)
- nth-child (56)
- math.pow (44)
- 原型和原型链 (63)
- canvas mdn (36)
- css @media (49)
- promise mdn (39)
- readasdataurl (52)
- if-modified-since (49)
- css ::after (50)
- border-image-slice (40)
- flex mdn (37)
- .join (41)
- function.apply (60)
- input type number (64)
- weakmap (62)
- js arguments (45)
- js delete方法 (61)
- blob type (44)
- math.max.apply (51)
- js (44)
- firefox 3 (47)
- cssbox-sizing (52)
- js删除 (49)
- js for continue (56)
- 最新留言
-