Add borders within table based on formula

PM_ME_YOUR_DATASETS

New Member
Joined
Sep 20, 2017
Messages
8
Hi all,

I've got a series of tables, one on each sheet, that generally look like this:

Code:
Number  Level  Percent
1       1.0    2
2       1.0    3
3       1.4    6
4       1.9    5
5       2.3    2
6       2.8    0
8       3.0    8
9       3.5    9
10      4.0    2

With "Number" appearing in cell A4.

What I'd like to be able to do is create a macro that conditionally applies a thick border inbetween cells, based on whether the leftmost value in the Level column (B) has changed. For example, I would like a thick border in between 1.9 and 2.3, between 2.8 and 3.0, and between 3.5 and 4.0. That border would extend to the right through the Percent column (C), but not to the left through the Number column (A). When the leftmost value in the Level column doesn't change, I would like to apply a thin border instead, but otherwise keep all existing borders in place.

The (row) length of the tables vary, as do the values.

Can anyone help me out?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Your answer lies within the conditional formatting area.
Use a formula that will set off the lines,
and instead of setting it to have a background fill color, which is what most people usually do...
Choose the tab of borders and tell it to give the cells a thick board if the formula is TRUE.
 
Upvote 0
Your answer lies within the conditional formatting area.
Use a formula that will set off the lines,
and instead of setting it to have a background fill color, which is what most people usually do...
Choose the tab of borders and tell it to give the cells a thick board if the formula is TRUE.
Is there a way to do this through VBA code?
 
Upvote 0
Got the answer figured out. For those who come later:

Code:
Sub ConditionalBorders()
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThick
    End With
    
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=LEFT($B5,1)=LEFT($B4,1)"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Borders(xlTop)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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