VBA Format Cell Font by ColumnLevel Grouping

Rowland Hamilton

Active Member
Joined
Nov 13, 2009
Messages
250
I don't want the text to show in a visible cell B1 when a group of adjoining columns is collapsed so I want to format the font in cell b1 white when the group is collapsed (level 1) and automatic = black when it is expanded (level 2). How can I?

Here are my first two attempts to piece this together (they don't work):

Code:
[FONT=Times New Roman][SIZE=3]Sub Format_With_Group_Collapse()[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]'[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Dim black As Long[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Dim white As Long[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]black = vbBlack[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]white = vbWhite[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Application.ScreenUpdating = False[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Sheets("Sheet1").Select[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]With ActiveSheet[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]If ActiveSheet.Outline.ShowLevels.ColumnLevels = 1 Then[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Range("b1").Font.Color = white[/SIZE][/FONT]
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>[FONT=Times New Roman][SIZE=3] [/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3]Else[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]If ActiveSheet.Outline.ShowLevels.ColumnLevels = 2 Then[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Range("b1").Font.Color = black[/SIZE][/FONT]
<o:p>[FONT=Times New Roman][SIZE=3] [/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3]End If[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]End With[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]

And my Second attempt:
Code:
[FONT=Times New Roman][SIZE=3]Sub Format_With_Group_Collapse()[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]'[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Application.ScreenUpdating = False[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Sheets("Sheet1").Select[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]With ActiveSheet[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]If ActiveSheet.Outline.ShowLevels.ColumnLevels = 1 Then[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Range("B1").Select[/SIZE][/FONT]
[SIZE=3][FONT=Times New Roman]    With Selection.Font[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]        .ColorIndex = 2[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    End With[/FONT][/SIZE]
<o:p>[FONT=Times New Roman][SIZE=3] [/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3]Else[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]If ActiveSheet.Outline.ShowLevels.ColumnLevels = 2 Then[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Range("b1").Select[/SIZE][/FONT]
[SIZE=3][FONT=Times New Roman]    With Selection.Font[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]         .ColorIndex = xlAutomatic[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    End With[/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3]End If[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]End With[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]

Any ideas? Thank you, Rowland
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top