VBA:将单元格格式设置为默认值的最佳方法

时间:2019-07-15 03:36:17

标签: excel vba formatting

我创建了一个函数,用于将一个或多个单元格设置为默认格式(即所有单元格在新工作表中的外观)。这是我的功能,

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

很明显,我的功能很长。因此,我想知道是否有可能以更简单的方式对其进行重构。例如。可以将字体,边框和室内对象重置为新工作表中的内容吗?

1 个答案:

答案 0 :(得分:0)

您可以使用 Range.ClearFormats -这将清除对象的格式(例如,字体,边框,内部颜色)。

https://docs.microsoft.com/en-us/office/vba/api/excel.range.clearformats