Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Programmatically Reset a Workbook to Default Styles


View products that this article applies to.

This article was previously published under Q247933
For a Microsoft Excel 2002 version of this article, see 291321 (http://support.microsoft.com/kb/291321/EN-US/ ) .
For a Microsoft Excel 98 version of this article, see 247981 (http://support.microsoft.com/kb/247981/EN-US/ ) .
For a Microsoft Excel 97 version of this article, see 247980 (http://support.microsoft.com/kb/247980/EN-US/ ) .
For a Microsoft Excel 7.0 version of this article, see 247982 (http://support.microsoft.com/kb/247982/EN-US/ ) .

↑ Back to the top


Summary

The following Microsoft Visual Basic for Applications Sub procedure removes all styles in a Microsoft Excel workbook and then adds back the default styles you see in a new workbook. This macro may be helpful for removing extra styles added to a workbook infected by a macro virus.

This macro uses the Workbooks.Add method, which bypasses templates in the startup directories. Because of this, it works correctly even if templates in the startup directories have had extra styles added to them.

NOTE: The Excel File Recovery Macro has an option to remove all formatting from a workbook. To find information about this macro, or to download this macro, go to the Office Resource Kit. For more information about the <I>Microsoft Office XP Resource Kit</I>, visit the following Microsoft Web site:

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following macro removes the styles from the currently active workbook. If you type this code into a new workbook, please make sure to activate the workbook from which you want to reset the styles before running the macro.
Sub RebuildDefaultStyles()

'The purpose of this macro is to remove all styles in the active
'workbook and rebuild the default styles. "Normal" cannot be
'deleted. Therefore the macro does not attempt to delete it.
'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
    
    'Delete all the styles in the workbook.
    For Each CurStyle In ActiveWorkbook.Styles
        If CurStyle.Name <> "Normal" Then CurStyle.Delete
    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
				

↑ Back to the top


References

For additional information about how to identify and remove the PLDT/CAR/SGV macro viruses, click the article number below to view the article in the Microsoft Knowledge Base:
213512� XL2000: How to Identify and Remove PLDT/CAR/SGV Macro Viruses
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:
212536� OFF2000: How to Run Sample Code from Knowledge Base Articles

↑ Back to the top


Keywords: KB247933, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 247933
Revision : 6
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 236