VBA绘制边界

以下是我在其中一个电子表格中用于执行格式设置的代码,并且没有任何错误。

这是有点冗长,因为我从macros录像机得到这个,并修改了一下。

我遇到的这个脚本的问题是大约需要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 

将边界看作线条:顶部,右侧,底部,左侧,垂直线(范围内)和水平线(范围内)

一行代码将绘制该范围的所有行。 你可以修改它们来得到你想要的。

要有一个干净的代码和更好的控制代码的执行,你应该学会编写函数,作为一个例子,我写了一个函数,给你在给定的工作表给定列中的最后一行。

 Sub DrawBorder() Dim lRow As Integer Dim cell As Range Dim rng As Range Dim WS As Worksheet Set WS = ActiveSheet 'you can set this to a specific sheet like Set WS=Sheets("Sheet1") 'Clear all of the borders in the sheet WS.Cells.Borders.LineStyle = xlNone 'Find the last row in column B=2 lRow = LastRowInColumn(WS, 2) Set rng = WS.Range("B5:D" & lRow) 'Borders of the cells inside the range rng.Borders.LineStyle = xlDot 'Border of the range as a whole with double lines rng.Borders(xlEdgeTop).LineStyle = xlDouble rng.Borders(xlEdgeBottom).LineStyle = xlDouble rng.Borders(xlEdgeLeft).LineStyle = xlDouble rng.Borders(xlEdgeRight).LineStyle = xlDouble ' 'You can use these lines to remove the vertical/horizontal lines isnide a range ' rng.Borders(xlInsideVertical).LineStyle = xlNone ' rng.Borders(xlInsideHorizontal).LineStyle = xlNone End Sub Function LastRowInColumn(Optional sh As Worksheet, Optional colNumber As Long = 1) As Long 'Finds the last row in a particular column which has a value in it If sh Is Nothing Then Set sh = ActiveSheet End If LastRowInColumn = sh.Cells(sh.Rows.Count, colNumber).End(xlUp).Row End Function