Beruflich Dokumente
Kultur Dokumente
Summary: Learn how to create professional, easily maintainable workbooks in Microsoft Office Excel 2007 by taking advantage of styles. See how to modify existing styles,
create your own styles, and use sample VBA subroutines to use styles to their fullest advantage. Helpful tips to working with styles are also presented. 8 printed pages
March 2009
Contents
Creating Styles
Conclusion
Additional Resources
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 1/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
By consistently using cell styles, instead of changing parts of the cell's formatting, you are forced to think about the structure of your work. Religiously using styles may
even force you to reconsider the overall structure of the entire spreadsheet model: The quality of the computational model itself may be positively affected.
In this article, you see how you can use styles to ease the design and maintenance of your spreadsheet models.
The use of styles takes some getting accustomed to but brings you great advantages. Imagine showing a nicely formatted sheet to your boss. Further imagine your boss
asking you to change all input cells having a darkyellow fill to a lightyellow background fill. For a large model, this might require a huge amount of work. However by
using consistent styles, you can update all cells in a matter of seconds.
Note
Styles are specific to a workbook. Thus if you assign a style, it stays with the workbook. However, you can copy a style from one workbook to another by simply copying
a cell with that style to the other workbook. Additionally, there is a Merge button on the Style dialog box see Figure 1 that lets you merge the styles from one
workbook into another workbook.
Styles are additive. Cell formatting is the sum of the applied style and all modifications to individual formatting elements on top of that style. Which parts of the available
formatting options are included in a style is determined during the definition of the style see the Style dialog box in Figure 1. In Microsoft Excel 97, Microsoft Excel 2000,
Microsoft Excel 2002, and Microsoft Office Excel 2003, you display the Style dialog by clicking the Format menu and then clicking Style. In Excel 2007, you access the
Style dialog box from the Home tab, in the Styles group, by clicking the Cell Styles button.
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 2/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
Excel 2007 has a slightly different Style dialog box as seen in Figure 2.
When you simultaneously apply multiple styles to a cell, the end result is the addition of both styles. Theoretically, you might think that this would enable cascading styles;
unfortunately, Excel does not retain the order of applied styles. Only the order of the last style selected is retained. Additionally, a style cannot be derived from other styles
while maintaining a link to the parent style. Changes to the original style are not reflected in the child styles.
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 3/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
Creating Styles
A convenient way to create a is
This documentation style is by selecting
archived a cell
and is not that maintained.
being has all of the formatting that you want, before opening the Style dialog screen.
In Excel 97 through 2003: Click the Format menu and then click Style.
In Excel 2007: Click the Home tab and in the Styles group, click the Cell Styles button.
2. In the Style Name box, type a name for the new style.
3. By default, all formatting elements are checked. Clear the checkboxes for the formatting elements you want to omit from the style you are creating.
4. Click the Modify button. The Format Cells dialog box appears as shown in Figure 3. Notice that the elements in the Style dialog box see Figure 1 or Figure 2 are
identical to the tabs on the Format Cells dialog box.
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 4/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
Note
When you change a formatting element on a tab in the Format Cells dialog box that was not selected on the Style dialog box, Excel automatically checks that
element for you; it becomes part of that style.
5. Select any format options you want for the new style and then click OK to return to the Style dialog box.
In Excel 2003, after modifying the style settings, you have two options to save the modifications:
By clicking the OK button, the selected style is modified and the currently selected cells have that style applied.
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 5/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
By clicking the Add button and then clicking Close, the style is updated but not applied to the currently selected cells. The cells that currently have this style
applied are updated regardless of which method you use. In other words, if you want to modify a style and apply it to the current selection, click OK.
This documentation is archived and is not being maintained.
Otherwise, click Add and then click Close.
Excel 2007 defaults to the Add option so that the style dialog is updated or added, but the style is not be applied to the selected cells.
In Excel 97, Excel 2000, Excel 2002, and Excel 2003, you can save yourself some time by adding the Style box to a toolbar.
1. Rightclick any toolbar or menu and then click Customize. Click the Commands tab as shown in Figure 4.
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 6/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
3. Next, drag the Style control from the Commands box to a toolbar as shown in Figure 5.
This documentation is archived and is not being maintained.
Figure 5. Drag the Style control to a toolbar
You are now ready to apply a style to cells from the new Style box.
In Excel 97, Excel 2000, Excel 2002, and Excel 2003, if the format of the selected cells deviates from the selected style, a dialog box is displayed that ask you whether or not
to update the style to match the formatting of the selected cells. Excel 2007 does not display this box.
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 7/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
If you click Yes, Excel checks all selected cells and only updates the styles that apply to all cells. For example, if you change a font characteristic for one of the
selected cells, Excel removes the Font settings from the styles for all of those cells. You can reset the style using the Format, Style menu and modify the style to
include that setting again. However, the original cell with the changed font remains different than the other cells.
If you click No, the selected style is applied to all cells. Any formatting in those cells is replaced with the style you chose. Any deviations from the style are removed.
In Excel 2007, this is the default behavior when you select a style from the Styles gallery.
Figure 7. A range of cells with one style where one cell deviates from that style
You can restore the style of the deviating cell by selecting the cell and choosing the style from the Style list and then clicking No to the confirmation dialog in Excel 97,
Excel 2000, Excel 2002, and Excel 2003. Note that in Excel 2007, the confirmation box is never displayed and the default action is the same as if you clicked No to the
confirmation.
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 8/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
If you need to adjust a style, select the cell in column B and adjust the style settings from there. This gives you a preview of the proposed change without actually
modifying the style in your working worksheets.
Creating a style based on an existing one is just as easy. Make a copy the applicable row and insert it in any blank area of the worksheet. Select the cell in column B of the
newly inserted row and click the Format menu, and then the Style menu. Type the name of the new style and click Add. Next click Modify to change the style details. And
finally, update the name in column A.
Caution
In Excel 2007, all styles which are not used anywhere in your workbook are not saved with your file. This only applies if you use the new Excel 2007 Open XML Format
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 9/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
files .xlsx, .xslm and .xlst when you save your file. To prevent this from happening, using a styles worksheet as mentioned earlier on is a good idea.
Use functional sets of styles. In most workbook models, cells fall into various categories:
Input cells: Cells that are the main data inputs to your model
Parameter cells: Cells that contain constants for your model, such as data boundaries
Output cells: Cells meant for output, such as printing or presenting the results of a calculation on screen
Boundary cells: Shading otherwise empty cells that make areas with differing functions stand out from other areas
Consider creating styles for each of these cell functions, such as giving each range of cells their own fill color. This is also a good time to consider whether a styles Locked
property needs to be set to off or on. By using a system such as this, it becomes very easy to maintain your workbooks. For example, imagine how easy it now becomes to
change a cell from an input to an output cell; just change its style and you are finished.
Find cells with a certain style. The following subroutine find cells with a style containing demo in its name.
VB
SubFindaStyle()
'
'Procedure:FindaStyle
'Company:JKPApplicationDevelopmentServices(c)
'Author:JanKarelPieterse
'Created:4102007
'Purpose:Searchesforastylebyname.
'
DimoShAsWorksheet
DimoCellAsRange
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 10/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
ForEachoShInThisWorkbook.Worksheets
ForEachoCellInoSh.UsedRange.Cells
IfoCell.StyleLike"*demo*"Then
This documentation is archived and is not being maintained.
Application.GoTooCell
Stop
EndIf
Next
Next
EndSub
When a cell is found with a style that matches that name filter, the code stops running, letting you examine the cell in detail. You can continue running the code by
pressing the F5 key.
Creating a list of styles. The following subroutine adds a table of your styles on a worksheet named Config Styles.
VB
SubListStyles()
'
'Procedure:ListStyles
'Company:JKPApplicationDevelopmentServices(c)
'Author:JanKarelPieterse
'Created:4102007
'Purpose:Createsatableofstylesthatareusedinaworkbook.
'
DimoStAsStyle
DimoCellAsRange
DimlCountAsLong
DimoStyleshAsWorksheet
SetoStylesh=ThisWorkbook.Worksheets("ConfigStyles")
WithoStylesh
lCount=oStylesh.UsedRange.Rows.Count+1
ForEachoStInThisWorkbook.Styles
OnErrorResumeNext
SetoCell=Nothing
SetoCell=Intersect(oStylesh.UsedRange,oStylesh.Range("A:A")).Find(oSt.Name,_
oStylesh.Range("A1"),xlValues,xlWhole,,,False)
IfoCellIsNothingThen
lCount=lCount+1
.Cells(lCount,1).Style=oSt.Name
.Cells(lCount,1).Value=oSt.NameLocal
.Cells(lCount,2).Style=oSt.Name
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 11/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
EndIf
Next
EndWith
This documentation is archived and is not being maintained.
EndSub
Clear all formatting of cells and reapply their styles. The following subroutine removes all of the formatting of all cells and then subsequently reapplies the style.
Caution
If you have not adhered to using styles strictly, you will lose all formatting in your file.
VB
SubReApplyStyles()
'
'Procedure:ReApplyStyles
'Company:JKPApplicationDevelopmentServices(c)
'Author:JanKarelPieterse
'Created:4102007
'Purpose:Removestheformattingfromallcellsandthenreappliesthestyle.
'
'Resetsstylesofcellstotheiroriginalstyle(resetsallformattingdoneontopofANYstyle)
DimoCellAsRange
DimoShAsWorksheet
IfMsgBox("Proceedwithcare:"&vbNewLine&vbNewLine&_
"Thisroutinewilleraseallformattingdoneontopoftheexistingcellstyles."&vbNewLine&_
"Continue?",vbCritical+vbOKCancel+vbDefaultButton2,GSAPPNAME)=vbOKThen
ForEachoShInActiveWindow.SelectedSheets
ForEachoCellInoSh.UsedRange.Cells
IfoCell.MergeArea.Cells.Count=1Then
oCell.Style=CStr(oCell.Style)
EndIf
Next
Next
EndIf
EndSub
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 12/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
Replace one style with another.The following subroutine uses a list with two columns. The column on the left contains the names of existing styles. The column on the
right contains the names of the style you want to replace them with. The code moves through the selected cells in the left column and checks if the style name in the
columnThis
to itsdocumentation
right differs. If so, you are prompted
is archived with
and is not the alternative
being maintained. name. Clicking OK causes the code to update ALL cells with the old style to the new style. Before
running this subroutine, you need to select the cells in the lefthand column.
VB
SubFixStyles()
'
'Procedure:FixStyles
'Company:JKPApplicationDevelopmentServices(c)
'Author:JanKarelPieterse
'Created:4102007
'Purpose:Replacesstyleswiththereplacementstyleasdefinedbyatwocolumnlist.
'column1shouldcontaintheexistingstyle,col2thereplacingstyle
'
DimsOldStAsString
DimsNewStAsString
DimoShAsWorksheet
DimoCellAsRange
DimoSourceCellAsRange
SetoSourceCell=ActiveCell
WhileoSourceCell.Value<>""
sOldSt=oSourceCell.Value
sNewSt=InputBox("Pleaseenterreplacementstylefor:"&sOldSt,"Stylechanger",oSourceCell.Offset(,1).Value)
IfsNewSt=""ThenExitSub
IfsNewSt<>""AndsNewSt<>sOldStThen
ForEachoShInThisWorkbook.Worksheets
ForEachoCellInoSh.UsedRange
IfoCell.Style=sOldStThen
Application.GoTooCell
OnErrorResumeNext
oCell.Style=sNewSt
EndIf
Next
Next
EndIf
SetoSourceCell=oSourceCell.Offset(1)
Wend
EndSub
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 13/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007
Conclusion
There is a lot to be gained by using styles in your Excel workbooks. For example, consider the following:
This documentation is archived and is not being maintained.
Ease of maintenance
Fewer problems with your file. For example, there is a limit on how many different cell formats Excel can handle. Managing how you use styles helps you avoid this
and other boundaries.
Additional Resources
For more information about using styles in Excel, see the following resources:
Frank Rice is a programming writer for the Microsoft Office Developer Center. He is a frequent contributor to the Office Talk column and to the Microsoft Developer
Network.
2017 Microsoft
https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 14/14