Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: Best Way of Setting Cell Formatting to Default

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?

like image 488
HarrisonO Avatar asked Oct 15 '25 18:10

HarrisonO


1 Answers

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

like image 60
Justyna MK Avatar answered Oct 17 '25 12:10

Justyna MK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!