|
 |
 |
 |
 |
Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms
of Use |
Trademarks
Article ID: 291321 - Last Review: May 8, 2007 - Revision: 5.0 How to programmatically reset a workbook to default stylesThis article was previously published under Q291321
For a Microsoft Excel 2000 version of this article, see 247933Â
(http://kbalertz.com/Feedback.aspx?kbNumber=247933/
)
.
For a Microsoft Excel 98 version of this article, see 247981Â
(http://kbalertz.com/Feedback.aspx?kbNumber=247981/
)
.
For a Microsoft Excel 97 version of this article, see 247980Â
(http://kbalertz.com/Feedback.aspx?kbNumber=247980/
)
.
For a Microsoft Excel 7.0 version of this article, see 247982Â
(http://kbalertz.com/Feedback.aspx?kbNumber=247982/
)
.
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 that is 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.
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. However, 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.
'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
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:
290140Â
(http://kbalertz.com/Feedback.aspx?kbNumber=290140/EN-US/
)
How to run sample code from Knowledge Base articles
APPLIES TO- Microsoft Office Excel 2007
- Microsoft Office Excel 2003
- Microsoft Excel 2002 Standard Edition
| kbdtacode kbhowto KB291321 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
|
 |
 |
 |
 |
 |
 |
 |
| |