2017-08-15 166 views
1

我希望下面的格式化单元格有更简单的代码。这段代码的目标是让一个盒子看起来更漂亮,没有什么过于花哨,只是简单而且这段代码看起来不简单。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 

我录制的宏很明显,但是,它很笨重,我相信它的一些可以取出来,只是不确定能做什么和不能取出/修改。

在此先感谢。

+2

你应该检查[如何避免使用'.select' /'.Activate'](https://stackoverflow.com/questions/10714251)。这会有很大的帮助。 – BruceWayne

+2

如果您的代码按预期工作,并且写得不好,或者感觉或*效率低下,那么就有** [codereview.se] **。 –

回答

5

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

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 
+0

Upvoted,但缩进有些问题。嵌套'与'块*需求* [正确的缩进](http://rubberduckvba.com/indentation);-)编辑:好多了! –

+0

@ Mat'sMug - 谢谢。我刚刚重新安排了代码 - 是否对它进行了排序?我看不出有什么不对,并且对于这种事情我很肛门! – SJR

+1

@SJR谢谢,它很好用! –

0

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

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