2017-08-15 165 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