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

printmediaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.