• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Add borders using Excel VBA

Hi all.

How can I add borders to every row or cell starting in the A3 cell and ending in column J using VBA? I will not know how many rows there are because that changes everytime.

Below is the code I use to format some of my columns. I would add it to that code.

Thank you in advance!
Dim rng2 As Long

With Worksheets("Sheet1")

    rng2 = .Range("A" & Rows.Count).End(xlUp).Row
    
    
    .Range("I3:I" & rng2).NumberFormat = "$ 0.0000"
    .Range("D3:D" & rng2).Columns.AutoFit
    .Range("J3:J" & rng2).Columns.AutoFit
    .Range("K3:K" & rng2).Columns.AutoFit
    .Range("L3:L" & rng2).Columns.AutoFit
    
    Set MR = Range("J3:J" & rng2)
    
    For Each cell In MR

    If UCase(cell.Value) = "SHEET" Then cell.Offset(, -9).Resize(, 12).Interior.ColorIndex = 36

    Next

End With

Open in new window

0
printmedia
Asked:
printmedia
  • 2
  • 2
1 Solution
 
StephenJRCommented:
You could do this:
With Range("A3", Range("A" & Rows.Count).End(xlUp)).Resize(, 10).Borders
    .LineStyle = xlContinuous
End With

Open in new window

0
 
broro183Commented:
nice one StephenJR,
I didn't even realise that we could use update the whole of the borders collection in one go. You'll be able to have a chuckle when you glance at my code (unedited) where I loop through the borders - I really don't know why your approach didn't occur to me???

Printmedia,
I was too slow to give an answer to your specific question because I decided to offer you an alternative to your current cell by cell looping at the same time. Anyway, here's what I put together which makes use of excel's native Autofilter functionality. This is usually faster than cell by cell looping.

Option Explicit

Sub Macro1()

Dim rng2 As Long
Dim brdr As Border
Dim MR As Range
Dim cell As Range
Dim oriAFMode As Boolean    'original Autofilter mode
Dim oriAFRng As Range    'original AutoFilterRange
Dim visNewAFRng As Range    'visible "new" AutoFilterRange (column J)

    With Worksheets("Sheet1")
        rng2 = .Range("A" & Rows.Count).End(xlUp).Row
        .Range("I3:I" & rng2).NumberFormat = "$ 0.0000"
        .Range("D3:D" & rng2).Columns.AutoFit
        .Range("J3:J" & rng2).Columns.AutoFit
        .Range("K3:K" & rng2).Columns.AutoFit
        .Range("L3:L" & rng2).Columns.AutoFit

        With .Range("a3:J" & rng2)
            For Each brdr In .Borders
                With brdr
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                End With
            Next brdr
        End With

        '*******************************
        'alternative to cell by cell looping, which should be faster...
        oriAFMode = .AutoFilterMode
        If oriAFMode Then
            Set oriAFRng = .AutoFilter.Range
            .AutoFilterMode = False
        End If

        '        'I have changed the next line to assume that it is on the same sheet as the other (above) processing
        '        'by adding a "." before range
        '        'NOTE: also changed from "J3" to "J2" to allow for a heading in the autofilter
        Set MR = .Range("J2:J" & rng2)
        '        For Each cell In MR
        '            If UCase(cell.Value) = "SHEET" Then cell.Offset(, -9).Resize(, 12).Interior.ColorIndex = 36
        '        Next cell

        'apply a filter & colour the cells
        With MR
            .AutoFilter Field:=1, Criteria1:="SHEET"
            On Error Resume Next
            Set visNewAFRng = .Offset(1, -9).Resize(.Rows.Count - 1, 12).SpecialCells(xlCellTypeVisible)
            If Not visNewAFRng Is Nothing Then
                If visNewAFRng.Areas.Count > 8000 Then
                    Stop 'read on...
                    'you may experience errors due to the limitations of specialcells see: http://www.rondebruin.nl/specialcells.htm
                    'this can be adjusted for, but will need some small additions to the code.
                End If
                visNewAFRng.Interior.ColorIndex = 36
            End If
        End With
        'turn off the single column AF
        .AutoFilterMode = False

        'reapply the autofilter to an original range (note: no previously active filters are restored)
        If oriAFMode Then
            oriAFRng.AutoFilter
        End If
        '*******************************

    End With

    Set visNewAFRng = Nothing
    Set oriAFRng = Nothing
    Set MR = Nothing
End Sub

Open in new window


hth
Rob
0
 
StephenJRCommented:
Rob - I think your approach is pretty good too. Tbh almost everything I do I do because I have seen someone cleverer than me do it. The only thing I have to do is as least even vaguely recall that I've seen it somewhere and then find it or rustle it up from the back of my memory!
0
 
broro183Commented:
Hi Stephen,

I work along the same lines & now I can add your trick to my repertoire ;-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now