Created
September 13, 2019 06:23
-
-
Save sethenoka/cc65744c72908cc211dc08f65b06ae78 to your computer and use it in GitHub Desktop.
A simple macro to quickly make the header row of an excel sheet prettier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Pretty_Headers() | |
' A simple macro to quickly make the header row of an excel sheet prettier | |
' Designed to: | |
' 1. Colour all header cells | |
' 2. Colour all header cell text | |
' 3. Bold all header cell text | |
' 4. Apply autofilter to the header row | |
' 5. Freeze the top row of the sheet | |
' | |
' The macro will apply to the active sheet | |
' ------------------------------------ | |
' @seth_enoka, www.sethenoka.com | |
Dim result, lColumn As String | |
'identify last column in header row and convert column number to letter | |
lColumn = Split(Cells(1, Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, _ | |
SearchDirection:=xlPrevious).Column).Address, "$")(1) & "1" | |
'if there are no values in the first (header) row, show an exception, then quit | |
If (Application.WorksheetFunction.CountA(ActiveSheet.Rows(1)) = 0) Then | |
result = MsgBox("There are no values in the header row. Enter one or more values and try again.", vbCritical, "No Values") | |
Exit Sub | |
'if there is only one value in the header row, beautify it | |
ElseIf (Application.WorksheetFunction.CountA(ActiveSheet.Rows(1)) = 1) Then | |
With Range(lColumn) | |
.Interior.ThemeColor = xlThemeColorLight1 | |
.Font.ThemeColor = xlThemeColorDark1 | |
.Font.Bold = True | |
.HorizontalAlignment = xlCenter | |
End With | |
Rows(1).AutoFilter | |
With ActiveWindow | |
.SplitColumn = 0 | |
.SplitRow = 1 | |
.FreezePanes = True | |
End With | |
'if there are multiple headers, beautify them all from column A to the last column in the row | |
ElseIf (Application.WorksheetFunction.CountA(ActiveSheet.Rows(1)) > 1) Then | |
With Range("A1", lColumn) | |
.Interior.ThemeColor = xlThemeColorLight1 | |
.Font.ThemeColor = xlThemeColorDark1 | |
.Font.Bold = True | |
.HorizontalAlignment = xlCenter | |
End With | |
Rows(1).AutoFilter | |
With ActiveWindow | |
.SplitColumn = 0 | |
.SplitRow = 1 | |
.FreezePanes = True | |
End With | |
'if any other state is achieved, which shouldn't be possible, display an error, then quit | |
Else | |
result = MsgBox("Reaching this state should not be possible. I'm impressed.", vbCritical, "Epic Fail") | |
Exit Sub | |
End If | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment