Sub topic 6
Choosing a style and style options
A table style controls how a table looks without changing the cells themselves. This chapter covers how to pick a style, how to switch its options on and off, and how to avoid the formatting problems that come up most often.
This chapter assumes you can already create a table from a range, as covered earlier in this tutorial, and that you are comfortable with the Ribbon.
What a table style does
A table style is a named set of formatting rules. It defines fonts, fills and borders for the parts of a table: the header row, the body rows, the first and last columns, and the total row. Excel applies the style to the table as a whole. If you add rows, the new rows pick up the same formatting.
Excel groups the built-in styles into three families.
| Family | Typical look | Name pattern |
|---|---|---|
| Light | Thin lines, little color | TableStyleLight1 to 21 |
| Medium | Colored header, banded rows | TableStyleMedium1 to 28 |
| Dark | Heavy fills, white text | TableStyleDark1 to 11 |
New tables get TableStyleMedium2 by default, a blue header with banded rows.
Choosing a style
- Click any cell inside the table. The Table Design tab appears on the Ribbon.
- In the Table Styles group, hover over a style. Excel previews it on the sheet.
- Click a style to apply it.
- Open the gallery with the small arrow at its lower right to see all styles.
To remove a style, open the gallery and choose Clear. The table keeps its structure, filter buttons and structured references. It only loses the style formatting.
Style options
The Table Style Options group holds checkboxes that change which parts of the style are drawn.
- Header Row shows or hides the header row. Hiding it does not delete the column names. Formulas that use structured references still work.
- Total Row adds or removes the total row. A later chapter covers it in detail.
- Banded Rows shades alternate rows.
- First Column and Last Column emphasize those columns, usually with bold text.
- Banded Columns shades alternate columns.
- Filter Button shows or hides the drop-down arrows in the header.
Setting the style with VBA
The TableStyle property of a ListObject takes the style name as a string. The macro below also sets the style options through the matching properties.
Sub StyleSalesTable()
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("SalesTable")
tbl.TableStyle = "TableStyleMedium9"
tbl.ShowTableStyleRowStripes = True
tbl.ShowTableStyleColumnStripes = False
tbl.ShowTableStyleFirstColumn = True
tbl.ShowTableStyleLastColumn = False
End SubAssigning an empty string, tbl.TableStyle = "", clears the style, like Clear in the gallery.
Common problems
Manual formatting overrides the style. A fill or font color that you apply directly to cells takes priority over the style. If you later change the style, those cells keep their old look. To reset them, select the table and use Clear Formats on the Home tab, then reapply the style. This also removes number formats, so reapply those afterward.
Banding is lost after hiding rows. Banded rows follow the row position in the table, not the visible rows. After you filter, the visible rows may not alternate neatly. This is expected behavior.
Custom styles. If your organization uses a standard look, choose New Table Style at the bottom of the gallery. A custom style is stored in the workbook, and you can set it as the default for new tables in that workbook.
Exercise
You apply a green fill by hand to a few cells in a table, then switch to a different table style. The green cells do not change. Why, and how do you fix it?
Show answer
Manual cell formatting takes priority over the table style, so the style cannot overwrite it. Select the table, choose Clear Formats on the Home tab (this also removes number formats, so reapply them), and then reapply the style.
Exercise
Which VBA property sets the style of a table, and which property turns banded rows on or off?
Show answer
The ListObject's TableStyle property sets the style, for example tbl.TableStyle = "TableStyleMedium2". The ShowTableStyleRowStripes property turns banded rows on or off.