VBA for thick cell border

MrzSanchez

New Member
Joined
Feb 13, 2017
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I havea column that need to apply thick border around individual cell if cell fromanother column (same row) meets criteria. Column K row 6 and below is thecolumn need to apply thick borer to. Column BK row 6 and below is the column itneeds to check if it contains specific content. example: if cell BK17 = 3 thenapply thick border around cell K17 only, and so on.


Icannot do this with Conditional Formatting because thick border is arequirement.

I'musing the following code but it boxes all cell in K column range not only thecells where the respective BK cell = 3.


Rich (BB code):
Sub Test()
    Dim LastRow As Long
    Dim r As Long
    With ActiveSheet
        LastRow =.Range("BK6").End(xlDown).Row
        For r = 6 To LastRow
            If .Range("BK" &r).Value = "3" Then
                With .Range("K6:K"& LastRow).Borders(xlTop)
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
                With .Range("K6:K"& LastRow).Borders(xlLeft)
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
                With .Range("K6:K"& LastRow).Borders(xlRight)
                     .LineStyle = xlContinuous
                     .Weight = xlThick
                     .ColorIndex = xlAutomatic
                End With
                With .Range("K6:K"& LastRow).Borders(xlBottom)
                     .LineStyle = xlContinuous
                     .Weight = xlThick
                     .ColorIndex = xlAutomatic
                End With
                With .Range("K6:K"& LastRow).Borders(xlTop)
                     .LineStyle = xlContinuous
                     .Weight = xlThick
                     .ColorIndex = xlAutomatic
                 End With

            End If
        Next r
    End With
End Sub





 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try
Code:
Sub Test()
    Dim LastRow As Long
    Dim r As Long
    With ActiveSheet
        LastRow = .Range("BK6").End(xlDown).Row
        For r = 6 To LastRow
            If .Range("BK" & r).Value = "3" Then
               With .Range("K" & r).Borders
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
            End If
        Next r
    End With
End Sub
If the BK column is numbers rather than text remove the quotes from the ="3"
 
Upvote 0
That is because you are looping through each row in column BK in your code, but then applying the thick border to the entire range, instead of just that one cell!
So, if any one cell is "3", the whole range will be get that formatting.

You should change all references of:
Code:
.Range("K6:K" & LastRow)
to
Code:
.Range("K" & r )
 
Upvote 0
Try
Code:
Sub Test()
    Dim LastRow As Long
    Dim r As Long
    With ActiveSheet
        LastRow = .Range("BK6").End(xlDown).Row
        For r = 6 To LastRow
            If .Range("BK" & r).Value = "3" Then
               With .Range("K" & r).Borders
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
            End If
        Next r
    End With
End Sub
If the BK column is numbers rather than text remove the quotes from the ="3"

You rock! Can't thank you enough!
 
Upvote 0
Of course!! hahaha so like me to over complicate things for myself. Thank you Mod!!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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