Sie sind auf Seite 1von 14

4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007

This documentation is archived and is not being maintained.

Using Styles to Dress Up Your Worksheets in Excel 2007


Office 2007

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

Jan Karel Pieterse, Excel Most Valuable Professional

Frank Rice, Microsoft Corporation

March 2009

Applies to: Microsoft Office Excel

Contents

Introduction to Excel Styles

How Cell Formatting Styles Work

Creating Styles

Applying Styles to a Cell

Deviating from a Style

Tips for Using Styles

Working with Styles in VBA

Conclusion

Additional Resources

https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 1/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007

About the Authors

This documentation is archived and is not being maintained.


Introduction to Excel Styles
Microsoft has made it very easy to polish your Microsoft Office Excel 2007 worksheets with various fill patterns, borders, and other options. Because the formatting of cells
is often done in an adhoc manner, many spreadsheet pages look less than professional.

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.

How Cell Formatting Styles Work


A style is a set of cell formatting settings which is given a name. All cells with a particular set of styles look the same. When you change a part of a style, all cells with that
style change their formatting accordingly.

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.

Figure 1. Styles dialog box in previous versions of Excel

https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 2/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007

This documentation is archived and is not being maintained.

Excel 2007 has a slightly different Style dialog box as seen in Figure 2.

Figure 2. Styles dialog box in Excel 2007

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.

To create a new style

1. Select the cell and do one of the following:

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.

Figure 3. Format Cells dialog box

https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 4/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007

This documentation is archived and is not being maintained.

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.

Applying Styles to a Cell


There are two methods to apply a style to a cell depending on which version of Excel you are using. In Excel 97, Excel 2000, Excel 2002, and Excel 2003, you click the
Format menu, click Style, and then select the style you want from the dropdown box. In Excel 2007, you click the Home tab, and in the Styles group, expand the Cell
Styles gallery, and then click a style.

In Excel 97, Excel 2000, Excel 2002, and Excel 2003, you can save yourself some time by adding the Style box to a toolbar.

To add the Style dropdown to a toolbar

1. Rightclick any toolbar or menu and then click Customize. Click the Commands tab as shown in Figure 4.

Figure 4. Customize dialog box

https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 6/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007

2. In the Categories box, select Format.

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.

Figure 6. Confirmation is needed to apply the style

https://msdn.microsoft.com/enus/library/dd537556(v=office.12).aspx 7/14
4/5/2017 UsingStylestoDressUpYourWorksheetsinExcel2007

This documentation is archived and is not being maintained.

This confirmation causes one of the following actions to occur:

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.

Deviating from a Style


If you apply a style to a range of cells and then change a formatting element for one of those cells, any further modifications to that particular style element is not applied
to the modified cell. For example, after changing a font attribute such as removing the bolding for a single cell, changing the font attributes of the style updates all cells in
the range except the one you previously modified see Figure 7.

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

Tips for Using Styles


Managing
This Styles. To assist you
documentation in adjusting
is archived andexisting styles or
is not being creating styles for a workbook, you might consider adding a blank worksheet to your workbook. In the new
maintained.
worksheet, put the names of the styles you are using in the workbook into column A and an example of that style in column B such as seen in Figure 8.

Figure 8. Table with styles in a worksheet

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.

This documentation is archived and is not being maintained.


Using styles. You should be judicious in modifying existing styles. Avoid modifying one formatting element of a cell with a style. Instead, consider if it is worth the effort to
add a new style. For example, if you have a style for percentage with two decimal places and you have a cell which requires three, then add a style for that purpose instead
of updating the existing style or just changing the cells number format on top of its style. Adapting this method likely forces you to consider what cell styles your
document needs, improving the consistency of your worksheets, and helping you to avoid adhoc style formatting.

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

Calculation cells: The cells where calculations are performed

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.

Working with Styles in VBA


The following Microsoft Visual Basic for Applications VBA subroutines greatly ease your work with styles and demonstrate how to work with the Style object.

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.

Consistent formatting of your models

Ease of maintenance

A strict use of styles leads to a structured way of working

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:

Style Object [Excel 2003 VBA Language Reference]

How to: Apply Styles to Ranges in Workbooks

Understanding the Excel Object Model from a .NET Developer's Perspective

About the Authors


Jan Karel Pieterse founded JKP Application Development Services in November 2003. JKP Application Development Services develops custommade applications using
Microsoft Office Excel and Microsoft Office Access, and VBA to its full extent.

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

Das könnte Ihnen auch gefallen