Add border to adjacent cell

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
268
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi, Need help to make the Part C code works ( the code is to add border to 2 adjacent cells if value condition of the two cells fall to the two variants gp1 and gp2 otherwise no border of cells (resume to normal).
Cell range in both Part B and C are the same.

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Select Case Sh.Name
    Case "Data"
      Exit Sub
    Case Else
  End Select

'PART A
'ProperCase in Column A

Dim LastRow As Long
LastRow = Range("A3").End(xlDown).Row

If Target.CountLarge > 1 Then Exit Sub
On Error GoTo ErrHandler:

If Not Intersect(Target, Range("A3:A" & LastRow)) Is Nothing Then
If Not IsNumeric(Target.Value) Then
Application.EnableEvents = False
Target.Value = StrConv(Target.Text, vbProperCase)
Application.EnableEvents = True
End If
End If



'PART B
'UpperCase From Range(C:AG)
If Not Intersect(Target, Range("C3:AG" & LastRow)) Is Nothing Then
If Not IsNumeric(Target.Value) Then
Application.EnableEvents = False
Target.Value = UCase(Target.Text)
Application.EnableEvents = True
End If
End If
ErrHandler:
Application.EnableEvents = True

'PART C
'Conditions to HIGHLIGHT Cells
If Not Intersect(Target, Range("C3:AG" & LastRow)) Is Nothing Then
If Not IsNumeric(Target.Value) Then
Application.EnableEvents = False
Dim c As Range
Dim gp1, gp2 As Variant
Dim rng As Range


gp1 = Array("D", "G")
gp2 = Array("E", "N")
'LastRow = Range("A3").End(xlDown).Row

For Each c In Range("C3", Range("AG" & Rows.Count).End(xlUp))
    If c.Value = gp1 And c.Offset(, -1).Value = gp2 Then
    Set rng = Range(c, c.Offset(, -1))
    
   'Add Border around Range
    rng.BorderAround LineStyle:=xlContinuous, Weight:=xlThick, ColorIndex:=32
    
Else
     'Remove All Borders if condition not meets
     rng.Borders.LineStyle = xlLineStyleNone
  End If
 
Next c
 
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

mse330

Well-known Member
Joined
Oct 18, 2007
Messages
738
Office Version
  1. 365
Platform
  1. Windows
For 'Part C' only ...
VBA Code:
'PART C
'Conditions to HIGHLIGHT Cells
If Not Intersect(Target, Range("C3:AG" & LastRow)) Is Nothing Then
   If Not IsNumeric(Target.Value) Then
      Application.EnableEvents = False
      Dim c As Range, rng As Range
      
      For Each c In Range("C3", Range("AG" & Rows.Count).End(xlUp))
         If (c = "D" Or c = "G") And (c.Offset(, -1) = "E" Or c.Offset(, -1) = "N") Then
            Set rng = Range(c, c.Offset(, -1))
            'Add Border around Range
            rng.BorderAround LineStyle:=xlContinuous, Weight:=xlThick, ColorIndex:=32
         Else
            'Remove All Borders if condition not meets
            rng.Borders.LineStyle = xlLineStyleNone
         End If
      Next c
   End If
End If
[/vba]
 

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
268
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi mse330,
it shows error
 

Attachments

  • errorC.png
    errorC.png
    16.4 KB · Views: 6

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
268
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi mse330, I need to create the criteria one by one to make it works. Thanks anyway.
 

mse330

Well-known Member
Joined
Oct 18, 2007
Messages
738
Office Version
  1. 365
Platform
  1. Windows
What was the error you were getting ?
 

Forum statistics

Threads
1,140,926
Messages
5,703,215
Members
421,282
Latest member
hogie

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
Top