I've created a function for setting a cell or cells to default formatting (i.e. what all cells look like in a new worksheet). Here's my function,
Public Function setDefaultCellFormat(ByVal worksheetName As String, ByVal target As Range)
Dim neutralTintAndShade As Single: neutralTintAndShade = 0
With Worksheets(worksheetName).target.Font
.color = xlWhite
.FontStyle = "Regular"
.Name = "Arial"
.Size = 10
.Strikethrough = False
.Underline = xlUnderlineStyleNone
.Subscript = False
.Superscript = False
End With
With Worksheets(worksheetName).target.Borders
.color = vbWhite
.LineStyle = xlLineStyleNone
.ThemeColor = xlThemeColorNone
.TintAndShade = neutralTintAndShade
.Weight = 1
End With
With Worksheets(worksheetName).target.Interior
.color = vbWhite
.Gradient = xlGradientNone
.InvertIfNegative = False
.Pattern = xlPatternNone
.PatternColor = xlWhite
.PatternThemeColor = xlWhite
.PatternTintAndSage = neutralTintAndShade
.ThemeColor = xlWhite
.TintAndShade = neutralTintAndShade
End With
End Function
Obviously, my function is quite lengthy. Hence, I was wondering if it's possible to refactor it in a simpler way. E.g. can the Font, Borders, and Interior objects be reset as if it was from a new worksheet?
You can use Range.ClearFormats - this will clear the formatting of the object (e.g. font, borders, interior colour).
https://learn.microsoft.com/en-us/office/vba/api/excel.range.clearformats
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With