1
下面是我使用其中一個電子表格進行格式設置的代碼,它的工作原理沒有任何錯誤。VBA繪製邊框
這是有點冗長,因爲我從宏錄像機得到這個,並修改了一下。
我遇到的這個腳本的問題是大約需要5到10秒才能完成工作。
無論如何縮短這段代碼並加快這個過程?
Sub FORMAT()
Application.ScreenUpdating = False
Range("B5:EM5000").Select
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
'''''
Range("B5:D5").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
'''''
Range("B5:c5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 3).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
'''''
Range("B5:c5").Select
Range(Selection, Selection.End(xlDown)).Resize(, 25).Offset(0, 5).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
'''''
Range("B5:c5").Select
Range(Selection, Selection.End(xlDown)).Resize(, 11).Offset(0, 27).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
'''''
'''''
Range("B5:l5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 39).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''''''''
'''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 50).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''''''''
'''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 60).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''''''''
'''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 70).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''''''''
'''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 80).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''''''''
'''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 90).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''''''''
'''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 100).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''''''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 110).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''''''''
''''''''''''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 120).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''''''''
''''''''''''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 130).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''
''''''''''''''
Range("B5:k5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 140).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
Range("B5").Select
Range(Selection, Selection.End(xlDown)).Offset(0, 38).Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlHairline
End With
''''''''
''''''''''''''
Range("AP5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Rows.AutoFit
''''''''''''''
Range("e:f").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "mmm-yy;@"
Range("g:h").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "#,##0"
''''''
Range("B5:EM5000").Select
With Selection.Font
.Name = "Calibri"
.SIZE = 8
End With
Application.ScreenUpdating = True
End Sub
這個問題有點廣泛的答案。但我建議不要將表單格式化爲最後一行。我相信你永遠不會使用完整的'1 048 576'行。將格式限制爲行1000或其他內容會更快。你不需要選擇單元格。您可以直接格式化它們'帶範圍(「B5:D5」)。邊界(xlEdgeLeft)'。 –
不要先選擇細胞,這是需要額外的時間。只需直接使用Range對象,例如'Range(「B5:C5」)。Borders.Selection.Borders(xlEdgeLeft).LineStyle = xlDouble'。有關詳細信息,請參閱[如何避免使用Excel VBA宏中的選擇](http://stackoverflow.com/a/10717999/1490783) –
@Peh 我有另一個將數據插入列B的VBA代碼:D,和行數不同,這就是我在這裏使用「最後一行」的原因。所以我認爲只寫'Range(「B5:D5」)'沒有「最後一行不是正確的方式來覆蓋列B中的所有數據:D – Sahal