48

I am trying to create a simple function that will add borders around every cell in a certain range. Using the wonderful recording this generates a ton of code which is quite useless. The code below will display a 'table' of data, around each cell in this range I would like to add a border. Online I haven't been able to find a simple or clear answer for this.

All help is much appreciated!

Set DT = Sheets("DATA")
endRow = DT.Range("F" & Rows.Count).End(xlUp).Row
result = 3

For I = 2 To endRow
    If DT.Cells(I, 6).Value = Range("B1").Value Then
        Range("A" & result) = DT.Cells(I, 6).Value
        Range("B" & result) = DT.Cells(I, 1).Value
        Range("C" & result) = DT.Cells(I, 24).Value
        Range("D" & result) = DT.Cells(I, 37).Value
        Range("E" & result) = DT.Cells(I, 3).Value
        Range("F" & result) = DT.Cells(I, 15).Value
        Range("G" & result) = DT.Cells(I, 12).Value
        Range("H" & result) = DT.Cells(I, 40).Value
        Range("I" & result) = DT.Cells(I, 23).Value
        result = result + 1
    End If
Next I
1
  • 1
    I edited my title seen as though it confused people.
    – CustomX
    Commented Oct 29, 2012 at 13:33

8 Answers 8

140

You only need a single line of code to set the border around every cell in the range:

Range("A1:F20").Borders.LineStyle = xlContinuous

It's also easy to apply multiple effects to the border around each cell.

For example:

Sub RedOutlineCells()
    Dim rng As Range

    Set rng = Range("A1:F20")

    With rng.Borders
        .LineStyle = xlContinuous
        .Color = vbRed
        .Weight = xlThin
    End With
End Sub
0
12

The following can be called with any range as parameter:

Option Explicit

Sub SetRangeBorder(poRng As Range)
    If Not poRng Is Nothing Then
        poRng.Borders(xlDiagonalDown).LineStyle = xlNone
        poRng.Borders(xlDiagonalUp).LineStyle = xlNone
        poRng.Borders(xlEdgeLeft).LineStyle = xlContinuous
        poRng.Borders(xlEdgeTop).LineStyle = xlContinuous
        poRng.Borders(xlEdgeBottom).LineStyle = xlContinuous
        poRng.Borders(xlEdgeRight).LineStyle = xlContinuous
        poRng.Borders(xlInsideVertical).LineStyle = xlContinuous
        poRng.Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End If
End Sub

Examples:

Call SetRangeBorder(Range("C11"))
Call SetRangeBorder(Range("A" & result))
Call SetRangeBorder(DT.Cells(I, 6))
Call SetRangeBorder(Range("A3:I" & endRow))
0
11

I have a set of 15 subroutines I add to every Coded Excel Workbook I create and this is one of them. The following routine clears the area and creates a border.

Sample Call:

Call BoxIt(Range("A1:z25"))

Subroutine:

Sub BoxIt(aRng As Range)
On Error Resume Next

    With aRng

        'Clear existing
        .Borders.LineStyle = xlNone

        'Apply new borders
        .BorderAround xlContinuous, xlThick, 0
        With .Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlMedium
        End With
        With .Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlMedium
        End With
    End With

End Sub
2
  • 1
    Great Template.
    – John Shaw
    Commented Oct 7, 2019 at 22:55
  • Thanks^^ Bold outline with Standard Dividing Lines.
    – Crazyd
    Commented Feb 22, 2022 at 23:01
6

Here's another way

Sub testborder()

    Dim rRng As Range

    Set rRng = Sheet1.Range("B2:D5")

    'Clear existing
    rRng.Borders.LineStyle = xlNone

    'Apply new borders
    rRng.BorderAround xlContinuous
    rRng.Borders(xlInsideHorizontal).LineStyle = xlContinuous
    rRng.Borders(xlInsideVertical).LineStyle = xlContinuous

End Sub
1

For adding borders try this, for example:

Range("C11").Borders(xlEdgeRight).LineStyle = xlContinuous
Range("A15:D15").Borders(xlEdgeBottom).LineStyle = xlContinuous

Hope that syntax is correct because I've done this in C#.

3
  • Doesn't really help, this will work on a range, but not per cell.
    – CustomX
    Commented Oct 29, 2012 at 13:00
  • and, what exactly is this: Range("C11").Borders(xlEdgeRight).LineStyle = xlContinuous or this: Range("A15:A15").Borders(xlEdgeBottom).LineStyle = xlContinuous
    – Sylca
    Commented Oct 29, 2012 at 13:15
  • Idk that's what you've created nothing provided in my initial code.
    – CustomX
    Commented Oct 29, 2012 at 13:22
1
xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid
xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid
xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid
xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid
1

You can also include this task within another macro, without opening a new one:

I don't put Sub and end Sub, because the macro contains much longer code, as per picture below

With Sheets("1_PL").Range("EF1631:JJ1897")
    With .Borders
    .LineStyle = xlContinuous
    .Color = vbBlack
    .Weight = xlThin
    End With
End With

part of a larger sub (not shown as picture because it's a big picture)

0

You can add border to any range in a very dynamic way by using the AddBorder and GetCurrentRegionStartingGivenCell function in Xatocode as follows:

' First you may define a worksheet level named range in starting cell and name it as rngData
Sub BorderExample()

    Dim rngData         As Range ' Range to sort

    Set rngData = GetCurrentRegionStartingGivenCell(shtData.Range("rngData"))

    Call AddBorder(rngData, EdgeLeft)
    Call AddBorder(rngData, EdgeTop)
    Call AddBorder(rngData, EdgeRight)
    Call AddBorder(rngData, EdgeBottom)
    Call AddBorder(rngData, InsideVertical)
    Call AddBorder(rngData, InsideHorizontal)
    Call AddBorder(rngData, DiagonalUp)
    Call AddBorder(rngData, DiagonalDown)
    Call ClearBorder(rngData)
    Call AddBorder(rngData, Box)
    Call AddBorder(rngData, AllBorder)
    Call AddBorder(rngData, AllBorder, xlDot) ' Linestyle
    Call AddBorder(rngData, AllBorder, , xlMedium) ' Thickness
    Call AddBorder(rngData, AllBorder, , , vbRed) ' Color
End Sub

You can read the complete article here

Not the answer you're looking for? Browse other questions tagged or ask your own question.