Simplify and modify code to add border to cells

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys,
Need recommendiation to make adjustments in two areas.
1. I want to add borders to two adjacent cells but my code only adds border to the first cell (see image provided),
2. Also there are four conditions which trigger the function and how to shorten/simplify it.
Please help.

VBA Code:
Option Explicit
Sub HighlightCells1()
Dim c As Range
Dim rng As Range

For Each c In Range("C3", Range("AG" & Rows.Count).End(xlUp))
         If c = "E" And c.Offset(, 1) = "D" Then
             Set rng = Range(c, c.Offset(, 1))
                With rng
                 .Borders.LineStyle = xlContinuous
                 .Borders.Weight = xlThick
                 .Borders.Color = vbBlue
                End With
        
         ElseIf c = "N" And c.Offset(, 1) = "D" Then
             Set rng = Range(c, c.Offset(, 1))
                With rng.Borders
                 .LineStyle = xlContinuous
                 .Weight = xlThick
                 .Color = vbBlue
                End With
        
         ElseIf c = "E" And c.Offset(, 1) = "G" Then
             Set rng = Range(c, c.Offset(, 1))
                With rng.Borders
                 .LineStyle = xlContinuous
                 .Weight = xlThick
                 .Color = vbBlue
                End With
                
         ElseIf c = "N" And c.Offset(, 1) = "G" Then
             Set rng = Range(c, c.Offset(, 1))
                With rng.Borders
                 .LineStyle = xlContinuous
                 .Weight = xlThick
                 .Color = vbBlue
                End With
          
         Else
              Set rng = Range(c, c.Offset(, 1))
                 'Remove All Borders if condition not meets
                 With rng.Borders
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                 End With
         End If
     Next c
End Sub
 

Attachments

  • borderchange.png
    borderchange.png
    6.8 KB · Views: 7

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Your code will be adding border to both cells but then it will remove it as the condition is not met on a later pass of the loop. Lets say C3 contains E and D3 D. That satisfies one of your conditions and both cells go blue. Now on another pass D3 is queried with E3. They dont pass any conditions so turn black.
 
Upvote 0
So do the black first then the blue:

VBA Code:
Option Explicit

Sub HighlightCells1()

Dim c As Range, rng As Range, rng1 As Range

Set rng = Range("C3", Range("AG" & Rows.Count).End(xlUp))

With rng.Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
End With

For Each c In rng
    Select Case UCase(c.Value)
        Case "E", "N"
            Select Case UCase(c.Offset(, 1).Value)
                Case "D", "G"
                    Set rng1 = c.Resize(1, 2)
                    With rng1
                        .Borders.LineStyle = xlContinuous
                        .Borders.Weight = xlThick
                        .Borders.Color = vbBlue
                    End With
            End Select
    End Select
Next
            
End Sub
 
Upvote 0
Hi steve the fish,
Thanks. Your code works. But I missed 1 condition to trigger the change which is "N" in the 1st cell and "E" at the adjacent cell. Appreciate you advise how to add this to the code.
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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