Highlight range in row based on text value (tick) - can't add border :(

Usually_Confused

New Member
Joined
Jan 15, 2022
Messages
7
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
Hi,

I have successfully used some borrowed code from this 2016 post (Here) to highlight in grey a row, columns B>M specifically, based on a text value "Champagne" in column 'B'.

Thank you to all those in the original post! Very helpful. All good so far. Nice, neat code.

I then wanted to remove all internal borders and set an thick outside border to these highlighted cells. A header, basically.

1657656597806.png


I amost gave up before posting for help but did eventually manage to get it to work as above. See the difference between Champage (as intended) and Sparkling Wine (before formatting).

But the code is horribly amatuer (which I fully admit to being) as I'm doing each border one-by-one with a macro recording as the starting basis. And I have WHITE,RED,ROSE and SPARKLING WINE to do both in Caps and sentence case. Which is going to make one lengthy, lengthy module if I continue to do it this way. Can anyone suggest how to shorten this, please?

VBA Code:
For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
   '  Cell.EntireRow.Interior.ColorIndex = 5
     Cell.Resize(, 12).Interior.Color = RGB(192, 192, 192)
  End If
Next
          
          ' borders Macro
'

    endrow = Range("B" & Rows.Count).End(xlUp).Row
  For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlDiagonalDown).LineStyle = xlNone
      End If
Next
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlDiagonalUp).LineStyle = xlNone
      End If
Next
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlEdgeLeft).LineStyle = xlNone
      End If
Next
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlEdgeTop).LineStyle = xlNone
      End If
Next
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlEdgeBottom).LineStyle = xlNone
      End If
Next
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlEdgeRight).LineStyle = xlNone
      End If
Next
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlInsideVertical).LineStyle = xlNone
      End If
Next
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlInsideHorizontal).LineStyle = xlNone
      End If
Next
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlDiagonalDown).LineStyle = xlNone
      End If
Next
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
  If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlDiagonalUp).LineStyle = xlNone
      End If
Next

    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
    If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlEdgeLeft).LineStyle = xlContinuous
        Cell.Resize(, 12).Borders(xlEdgeLeft).ColorIndex = 0
        Cell.Resize(, 12).Borders(xlEdgeLeft).TintAndShade = 0
        Cell.Resize(, 12).Borders(xlEdgeLeft).Weight = xlMedium
    End If
Next
    
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
    If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlEdgeTop).LineStyle = xlContinuous
        Cell.Resize(, 12).Borders(xlEdgeTop).ColorIndex = 0
        Cell.Resize(, 12).Borders(xlEdgeTop).TintAndShade = 0
        Cell.Resize(, 12).Borders(xlEdgeTop).Weight = xlMedium
    End If
Next


'    With Cell.Resize(, 12).Borders(xlEdgeTop)
'        .LineStyle = xlContinuous
'        .ColorIndex = 0
'        .TintAndShade = 0
'        .Weight = xlMedium
'    End With

    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
    If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlEdgeBottom).LineStyle = xlContinuous
        Cell.Resize(, 12).Borders(xlEdgeBottom).ColorIndex = 0
        Cell.Resize(, 12).Borders(xlEdgeBottom).TintAndShade = 0
        Cell.Resize(, 12).Borders(xlEdgeBottom).Weight = xlMedium
    End If
Next

'
'    With Cell.Resize(, 12).Borders(xlEdgeBottom)
'        .LineStyle = xlContinuous
'        .ColorIndex = 0
'        .TintAndShade = 0
'        .Weight = xlMedium
'    End With
    
    endrow = Range("B" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("B10:B" & endrow)
    If Cell.Value = "Champagne" Then
    Cell.Resize(, 12).Borders(xlEdgeRight).LineStyle = xlContinuous
        Cell.Resize(, 12).Borders(xlEdgeRight).ColorIndex = 0
        Cell.Resize(, 12).Borders(xlEdgeRight).TintAndShade = 0
        Cell.Resize(, 12).Borders(xlEdgeRight).Weight = xlMedium
    End If
Next


'    With Cell.Resize(, 12).Borders(xlEdgeRight)
'        .LineStyle = xlContinuous
'        .ColorIndex = 0
'        .TintAndShade = 0
'        .Weight = xlMedium
'    End With
    Cell.Resize(, 12).Borders(xlInsideVertical).LineStyle = xlNone
    Cell.Resize(, 12).Borders(xlInsideHorizontal).LineStyle = xlNone
 ' End If
 ' End With
'  End If
'Next

End Sub

Also, the reference word (i.e. Champagne is always in Column 'B' but I'd really like to apply the grey formatting and the thick outside border from Column A if possible. I tried adding +1 and -1 in this bit:
"Cell.Resize(xxxx, 12)" but that didn't work. Any tips on that too would be greatly appreciated!

With thanks,

UsuallyConfused :unsure:
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
With the LCase function it always compares to lowercase, so it doesn't matter if it's uppercase or lowercase.

Try this:
VBA Code:
Sub HighlightRange()
  Dim cell As Range
  
  For Each cell In Range("B10", Range("B" & Rows.Count).End(3))
    Select Case LCase(cell.Value)
      Case LCase("Champagne"), LCase("SPARKLING WINE"), LCase("WHITE WINE"), LCase("Red Wine")
        With cell.Offset(, -1).Resize(, 13)
          .Interior.Color = RGB(192, 192, 192)
          .Borders.LineStyle = xlNone
          .Borders.LineStyle = xlContinuous
          .Borders.Weight = xlMedium
          .Borders(xlInsideVertical).LineStyle = xlNone
        End With
    End Select
  Next
End Sub
 
Upvote 0
With the LCase function it always compares to lowercase, so it doesn't matter if it's uppercase or lowercase.

Try this:
VBA Code:
Sub HighlightRange()
  Dim cell As Range
 
  For Each cell In Range("B10", Range("B" & Rows.Count).End(3))
    Select Case LCase(cell.Value)
      Case LCase("Champagne"), LCase("SPARKLING WINE"), LCase("WHITE WINE"), LCase("Red Wine")
        With cell.Offset(, -1).Resize(, 13)
          .Interior.Color = RGB(192, 192, 192)
          .Borders.LineStyle = xlNone
          .Borders.LineStyle = xlContinuous
          .Borders.Weight = xlMedium
          .Borders(xlInsideVertical).LineStyle = xlNone
        End With
    End Select
  Next
End Sub
Amazing! Thank you so much. Works perfectly. And you also fixed the offset to Column A

Me and my colleagues were able to laugh long and loud at my amateur attempt versus your slick, professional version 🤣
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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