1

I use this code from macros recorder and add inside a for loop.

How can do better this code if is possible ?

I want use both 2003 and 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
Filburt
  • 17,626
  • 12
  • 64
  • 115
KyriakosGian
  • 79
  • 1
  • 2
  • 8

2 Answers2

3

Further to my comment above which includes a link which talks in detail on avoiding .Select.

Here are my three cents.

A. Declare your objects/Variables

It becomes easier to work when you have declared your Variables/objects. This ensures that you do not have to type repetitive code. For example

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. Ensure that you fully qualify your objects and work with them

This is the most common cause of errors. Consider this line.

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

Here Excel is assuming that you are working with the current sheet. What if you are not. See this example

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

Here the Cells() object are not fully qualified and can cause an error. See this post

C. Cut out the extra/repetitive code

The Excel constants xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight etc each is equal to a number and that too in increasing order. If you type it in Immediate window then you can check it's value

'~~> This will give you 7
?xlEdgeLeft

So we can actually take advantage of this and shorten out code.

See the code below

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
Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
1

Try using the range like this

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

Do this for each border edge and colour etc

Steven Marciano
  • 130
  • 1
  • 9