borders based on cell value

williamu

New Member
Joined
Mar 19, 2019
Messages
16
VBA code to put borders base on cell value

if cell B7 > 0 green border
If cell B7 < 0 red border
outside borders only go
around B4 to B8

I want to do this for B7,
around B4 to B8
D7,
around D4 to D8
F7,
around F4 to F8
H7,
around H4 to H8
J7, around J4 to J8
L7, around L4 to L8
N7,
around B4 to B8
P7,
around B4 to B8
R7,
around B4 to B8
<strike>
</strike>
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi & welcome to MrExcel
How about
Code:
Sub williamu()
   Dim i As Long
   
   For i = 2 To 18 Step 2
     Cells(4, i).Resize(5).BorderAround , xlMedium, , IIf(Cells(7, i) > 0, vbGreen, vbRed)
   Next i
End Sub
 
Upvote 0
Pretty much the same, except for = 0 and this is event code that runs when changes are made to one of the cells on row 7.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Dim rng As Range
Set rng = Range("B7, D7, F7, H7, J7, L7, N7, P7, R7")
If Not Intersect(Target, rng) Is Nothing Then
    For Each c In rng
        If c < 0 Then
            c.Offset(-3).Resize(5).BorderAround xlContinuous, xlMedium, 10
        ElseIf c > 0 Then
            c.Offset(-3).Resize(5).BorderAround xlContinuous, xlMedium, 3
        Else
            If Not c.Borders.LineStyle = xlNone Then
                c.Offset(-3).Resize(5).Borders.LineStyle = xlNone
            End If
        End If
    Next
End If
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top