VBA – 更简单的格式代码

我希望下面的格式化单元格有更简单的代码。 这个代码的目标是使一个盒子看起来更漂亮,没有什么过于花哨,只是简单,这个代码看起来不简单。

Range("C17:C25").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With Range("A17:C25").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With Range("C17:C25").Select With Selection.Font .ColorIndex = xlAutomatic .Name = "Arial" .Size = 12 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .TintAndShade = 0 .ThemeFont = xlThemeFontNone End With With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With With Selection.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With 

我明显地logging了macros,然而,它非常笨重,我相信其中一些可以被拿出来,只是不确定什么可以被取出/修改。

提前致谢。

尝试这个。 你也可以免去select。 除非您有以前的设置,并且看起来好像是由macroslogging器生成的,否则最后的大部分格式化可能是多余的。

 Sub x() With Range("C17:C25") With .Borders .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Font .ColorIndex = xlAutomatic .Name = "Arial" .Size = 12 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .TintAndShade = 0 .ThemeFont = xlThemeFontNone End With .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False With .Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With End With With Range("A17:C25") .BorderAround LineStyle:=xlContinuous, ColorIndex:=0, Weight:=xlMedium With .Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With End With End Sub 

我再次将它重新格式化成更简单的东西。 我喜欢一些反馈,以确保我没有做任何禁忌。

 With Range("C17:C25") With .Borders .LineStyle = xlContinuous .Weight = xlThin End With With .Font .ColorIndex = xlAutomatic .Name = "Arial" .Size = 12 End With .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom With .Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With End With With Range("A17:C25") .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium With .Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin End With End With