2013-04-06 36 views
1

我从宏记录器使用此代码并添加for循环。如何从宏记录器改进代码?

如果可能的话,如何更好地执行此代码?

我想用2003年和2010年

Range(Cells(2, 2).Address, Cells(5, 5).Address).Select 
Selection.Borders(xlDiagonalDown).LineStyle = xlNone 
Selection.Borders(xlDiagonalUp).LineStyle = xlNone 
With Selection.Borders(xlEdgeLeft) 
    .LineStyle = xlDouble 
    .Color = -16777216 
    .Weight = xlThick 
End With 
With Selection.Borders(xlEdgeTop) 
    .LineStyle = xlDouble 
    .Color = -16777216 
    .Weight = xlThick 
End With 
With Selection.Borders(xlEdgeBottom) 
    .LineStyle = xlDouble 
    .Color = -16777216 
    .Weight = xlThick 
End With 
With Selection.Borders(xlEdgeRight) 
    .LineStyle = xlDouble 
    .Color = -16777216 
    .Weight = xlThick 
End With 
With Selection.Borders(xlInsideVertical) 
    .LineStyle = xlContinuous 
    .ColorIndex = xlAutomatic 
    .Weight = xlThin 
End With 
With Selection.Borders(xlInsideHorizontal) 
    .LineStyle = xlContinuous 
    .ColorIndex = xlAutomatic 
    .Weight = xlThin 
End With 
+0

代码你怎么想使代码更优秀?代码有什么问题? – stormCloud 2013-04-06 11:53:26

+0

http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select/10718179#10718179 – 2013-04-06 12:25:34

+0

为了改善它,我会说永远不要使用选择时使用范围 – 2013-04-06 12:39:35

回答

3

继我上面的评论其中包括一个link,其中详细谈到了避免.Select

这里是我的three美分。

A.声明你的对象/变量

它变得更容易,当你宣布你的变量/对象打交道。这可确保您不必输入重复的代码。例如

Range(Cells(2, 2).Address, Cells(5, 5).Address).THIS 
Range(Cells(2, 2).Address, Cells(5, 5).Address).THAT 
Range(Cells(2, 2).Address, Cells(5, 5).Address).THIS 

etc... 

B.确保您完全符合你的对象,并与他们

工作,这是错误的最常见原因。考虑这条线。

Range(Cells(2, 2).Address, Cells(5, 5).Address) 

这里Excel假设您正在使用当前工作表。如果你不是。看到这个例子

Sheets(2).Range(Cells(2, 2).Address, Cells(5, 5).Address) 

这里Cells()对象不完全合格,可能会导致错误。看到此post

C.剪切掉多余的/重复的代码

Excel的常数xlEdgeLeftxlEdgeTopxlEdgeBottomxlEdgeRight等各等于一个数量和太以递增顺序。如果您在立即窗口中键入它,那么你可以检查它的价值

'~~> This will give you 7 
?xlEdgeLeft 

所以我们实际上可以利用这一优势,缩短了代码。

见下

Option Explicit 

Sub Sample() 
    Dim ws As Worksheet 
    Dim rng As Range 
    Dim i As Long 

    '~~> Change this to the relevant worksheet 
    Set ws = ThisWorkbook.Sheets("Sheet1") 

    With ws 
     Set rng = .Range(.Cells(2, 2).Address, .Cells(5, 5).Address) 

     With rng 
      .Borders(xlDiagonalDown).LineStyle = xlNone 
      .Borders(xlDiagonalUp).LineStyle = xlNone 

      For i = 7 To 10 
       'xlEdgeLeft = 7 : xlEdgeTop = 8 : xlEdgeBottom = 9 
       'xlEdgeRight = 10 
       With .Borders(i) 
        .LineStyle = xlDouble: .Color = -16777216: .Weight = xlThick 
       End With 
      Next 

      For i = 11 To 12 
       'xlInsideVertical = 11 : xlInsideHorizontal = 12 
       With .Borders(i) 
        .LineStyle = xlContinuous: _ 
        .ColorIndex = xlAutomatic: .Weight = xlThick 
       End With 
      Next 
     End With 
    End With 
End Sub 
+0

我还会提及“避免使用'.Select'方法”(你已经在做) – RBarryYoung 2013-04-06 16:45:07

+0

Barry,我已经做了:)请参阅问题 – 2013-04-06 16:45:44

+0

下面的评论中的链接。然而,我将在上面的帖子中添加该内容。 – 2013-04-06 16:46:22

1

尝试使用范围这样

Range(Cells(2, 2).Address, Cells(5, 5).Address).Borders(xlEdgeBottom).LineStyle = xlDouble 

这样做对每个边界边缘及颜色等