删除Excel工作簿中的自定义样式

ghhaqwfi  于 2023-02-10  发布在  其他
关注(0)|答案(2)|浏览(176)

我在一个大型xlsb中工作(Microsoft Excel二进制工作簿),并注意到文件中保存了大量自定义样式。我认为这些样式都是从其他工作簿复制工作表时无意中包含在文件中的。我希望删除所有自定义样式,但手动方法除外(右键单击并删除)不起作用。然后我在网上搜索VBA解决方案,找到了下面的三个宏,但这些也不起作用。当我运行宏时,它们只是立即跳转到MsgBox,而没有删除任何自定义样式。希望有人在这里可以阐明一些我如何可以重新配置宏或指导我走向另一个解决方案。-〉见下面的更新信息!

Sub clear_all_styles()
 Dim styT As Style
 On Error Resume Next
 For Each styT In ActiveWorkbook.Styles
     If Not styT.BuiltIn Then
         If styT.Name <> "1" Then styT.Delete
     End If
 Next styT
 MsgBox "Macro completed" 'can be commented out
End Sub

Sub DeleteStyles()
    On Error Resume Next
    For Each sty In ActiveWorkbook.Styles
        If Not sty.BuiltIn Then
            sty.Delete
        End If
    Next sty
    MsgBox "Macro completed" 'can be commented out
End Sub

Sub StyleKiller()
    Dim st As Style
    On Error Resume Next
    For Each st In ActiveWorkbook.Styles
      If Not st.BuiltIn Then
        st.Delete
      End If
    Next
    On Error GoTo 0
    MsgBox "Macro completed" 'can be commented out
End Sub

更新信息:在线搜索更多解决方案后(通常,这些都不起作用)我终于找到了一个更大的解决方案,它起作用了(包括下面)。这个解决方案删除了绝大多数的自定义样式,但大约20个样式仍然不能删除的宏或手动操作。我注意到,所有这些剩余的自定义样式似乎有特殊的标志,我想知道这是否是他们不能't被删除。如果有人能提供任何指导,这是将不胜感激!

Sub RebuildDefaultStyles()
    
    'The purpose of this macro is to remove all styles in the active
    'workbook and rebuild the default styles.
    'It rebuilds the default styles by merging them from a new workbook.
    
    'Dimension variables.
       Dim MyBook As Workbook
       Dim tempBook As Workbook
       Dim CurStyle As Style
    
       'Set MyBook to the active workbook.
       Set MyBook = ActiveWorkbook
       On Error Resume Next
       'Delete all the styles in the workbook.
       For Each CurStyle In MyBook.Styles
          'If CurStyle.Name <> "Normal" Then CurStyle.Delete
          Select Case CurStyle.Name
             Case "20% - Accent1", "20% - Accent2", _
                   "20% - Accent3", "20% - Accent4", "20% - Accent5", "20% - Accent6", _
                   "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4", _
                   "40% - Accent5", "40% - Accent6", "60% - Accent1", "60% - Accent2", _
                   "60% - Accent3", "60% - Accent4", "60% - Accent5", "60% - Accent6", _
                   "Accent1", "Accent2", "Accent3", "Accent4", "Accent5", "Accent6", _
                   "Bad", "Calculation", "Check Cell", "Comma", "Comma [0]", "Currency", _
                   "Currency [0]", "Explanatory Text", "Good", "Heading 1", "Heading 2", _
                   "Heading 3", "Heading 4", "Input", "Linked Cell", "Neutral", "Normal", _
                   "Note", "Output", "Percent", "Title", "Total", "Warning Text"
                'Do nothing, these are the default styles
             Case Else
                CurStyle.Delete
          End Select
    
       Next CurStyle
    
       'Open a new workbook.
       Set tempBook = Workbooks.Add
    
       'Disable alerts so you may merge changes to the Normal style
       'from the new workbook.
       Application.DisplayAlerts = False
    
       'Merge styles from the new workbook into the existing workbook.
       MyBook.Styles.Merge Workbook:=tempBook
    
       'Enable alerts.
       Application.DisplayAlerts = True
    
       'Close the new workbook.
       tempBook.Close
    
    End Sub
fnx2tebb

fnx2tebb1#

通过子例程删除可能会提供更多信息:

Option Explicit

Sub DelStyles()
Dim st As Style
For Each st In ActiveWorkbook.Styles
    If Not st.BuiltIn Then
      DelSingleStyle st
    End If
Next
End Sub

Sub DelSingleStyle(st As Style)
On Error GoTo BadStyle
Dim s As String
s = st.Name
Debug.Print " Try to delete: "; s
st.Delete
Debug.Print " Deleted: "; s
Exit Sub
BadStyle:
Debug.Print "Not Deleted: "; st.Name
End Sub
jmp7cifd

jmp7cifd2#

只要对上面列出的Sub之一进行少量修改,所有样式,包括内置样式(“普通”除外,它不能删除)都将从工作簿中删除。

Sub DeleteStyles()
    Dim sty As Style

    'On Error Resume Next 'outcommented as it is not needed
    For Each sty In ActiveWorkbook.Styles
        'If Not sty.BuiltIn Then     'outcommented as all styles should be deleted
        If sty.Name <> "Normal" Then 'except Normal, which cannot be deleted
            sty.Delete
        End If
    Next sty
    
    MsgBox "Macro completed" 'can be commented out
End Sub

你从来没有回答我的问题,你是什么意思与“特殊符号”,但如果你的意思是%符号,出现在一些内置的样式,这只是百分比(惊喜!)的颜色填充,例如“20% -Accent 1”的意思是一个轻填充。

相关问题