VBA code with Formula that applies a Conditional Formatting border to a range of cells

Uhrlaff

New Member
Joined
May 17, 2019
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I'm creating a process for a group of users and want to make it user-friendly, and I already have an existing Macro which performs about 35 steps. I want to add one last step that applies a thin-line bottom border to certain cells in a range, and based upon a formula.
The conditional formatting can be applied/stored in any cell in row 2 such as A2, B2, D2, etc. and the formula is as follows: =COUNTIF($A1, "*Match*")
and the range in which to apply the bottom border is: =$A$2:$BG$5000

I believe the below code is a pretty good start, but I need help defining it correctly. Thank you so much.

Sub VBA_borders()
Dim iRange as Range
Dim iCells as Range
Set iRange = ThisWorkbook.ActiveSheet.UsedRange

For each iCells in iRange
If Not isEmpty(iCells) Then
Borders:=x1EdgeBottom, _
LineStyle:=x1Continuous, _
Weight:=x1Thin, _
ColorIndex:=x1Automatic

thanks,
David
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I'm replying to my own original question, and I'm just uploading an image of what I'm looking for.
 

Attachments

  • BottomBorder.png
    BottomBorder.png
    20 KB · Views: 17
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Test this with a copy of your workbook.

VBA Code:
Sub Add_Underlines()
  Dim rFound As Range
  Dim FirstAddr As String
  
  Set rFound = Columns("A").Find(What:="Match", LookAt:=xlPart, MatchCase:=False)
  If Not rFound Is Nothing Then
    Application.ScreenUpdating = False
    FirstAddr = rFound.Address
    Do
      rFound.Offset(1).Resize(, 4).Borders(xlEdgeBottom).LineStyle = xlContinuous
      Set rFound = Columns("A").Find(What:="Match", After:=rFound, LookAt:=xlPart, MatchCase:=False)
    Loop Until rFound.Address = FirstAddr
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Thank you Peter.
I will test your code on Monday. I'm in New York time zone. Take care!
David
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Test this with a copy of your workbook.

VBA Code:
Sub Add_Underlines()
  Dim rFound As Range
  Dim FirstAddr As String
 
  Set rFound = Columns("A").Find(What:="Match", LookAt:=xlPart, MatchCase:=False)
  If Not rFound Is Nothing Then
    Application.ScreenUpdating = False
    FirstAddr = rFound.Address
    Do
      rFound.Offset(1).Resize(, 4).Borders(xlEdgeBottom).LineStyle = xlContinuous
      Set rFound = Columns("A").Find(What:="Match", After:=rFound, LookAt:=xlPart, MatchCase:=False)
    Loop Until rFound.Address = FirstAddr
    Application.ScreenUpdating = True
  End If
End Sub
Peter, this code worked perfectly for me, so thank you so much. One additional thing I would like to do is add a red-colored line border for the case for a different word, namely "Missing".
I tried to set the line color and even had trouble with this.
David
 
Upvote 0
Firstly, what about the first point in my previous post?

Secondly, could we again have some sample data and results so we can see what sort of things we could expect in the sheet & exactly where the red line(s) should be?
 
Upvote 0
Firstly, what about the first point in my previous post?

Secondly, could we again have some sample data and results so we can see what sort of things we could expect in the sheet & exactly where the red line(s) should be?
I have updated my account details. And I have attached a screenshot for sample data regarding the red line. Thanks so much
 

Attachments

  • BottomBorder (2).png
    BottomBorder (2).png
    22.6 KB · Views: 9
Upvote 0
I have updated my account details.
Thanks for that. (y)


.. and for the sample.
I'm not sure if you wanted an exact match for "Missing". The code below still looks for a partial match so it would underline "There was a missing part". If you don't want that then try changing LookAt:=xlPart to LookAt:=xlWhole in the bottom section of thee code (in two places).

VBA Code:
Sub Add_Underlines_v2()
  Dim rFound As Range
  Dim FirstAddr As String
  
  Set rFound = Columns("A").Find(What:="Match", LookAt:=xlPart, MatchCase:=False)
  If Not rFound Is Nothing Then
    Application.ScreenUpdating = False
    FirstAddr = rFound.Address
    Do
      rFound.Offset(1).Resize(, 4).Borders(xlEdgeBottom).LineStyle = xlContinuous
      Set rFound = Columns("A").Find(What:="Match", After:=rFound, LookAt:=xlPart, MatchCase:=False)
    Loop Until rFound.Address = FirstAddr
    Application.ScreenUpdating = True
  End If
  
  Set rFound = Columns("A").Find(What:="Missing", LookAt:=xlPart, MatchCase:=False)
  If Not rFound Is Nothing Then
    Application.ScreenUpdating = False
    FirstAddr = rFound.Address
    Do
      With rFound.Resize(, 4).Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Color = vbRed
      End With
      Set rFound = Columns("A").Find(What:="Missing", After:=rFound, LookAt:=xlPart, MatchCase:=False)
    Loop Until rFound.Address = FirstAddr
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Solution
Thanks for that. (y)


.. and for the sample.
I'm not sure if you wanted an exact match for "Missing". The code below still looks for a partial match so it would underline "There was a missing part". If you don't want that then try changing LookAt:=xlPart to LookAt:=xlWhole in the bottom section of thee code (in two places).

VBA Code:
Sub Add_Underlines_v2()
  Dim rFound As Range
  Dim FirstAddr As String
 
  Set rFound = Columns("A").Find(What:="Match", LookAt:=xlPart, MatchCase:=False)
  If Not rFound Is Nothing Then
    Application.ScreenUpdating = False
    FirstAddr = rFound.Address
    Do
      rFound.Offset(1).Resize(, 4).Borders(xlEdgeBottom).LineStyle = xlContinuous
      Set rFound = Columns("A").Find(What:="Match", After:=rFound, LookAt:=xlPart, MatchCase:=False)
    Loop Until rFound.Address = FirstAddr
    Application.ScreenUpdating = True
  End If
 
  Set rFound = Columns("A").Find(What:="Missing", LookAt:=xlPart, MatchCase:=False)
  If Not rFound Is Nothing Then
    Application.ScreenUpdating = False
    FirstAddr = rFound.Address
    Do
      With rFound.Resize(, 4).Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Color = vbRed
      End With
      Set rFound = Columns("A").Find(What:="Missing", After:=rFound, LookAt:=xlPart, MatchCase:=False)
    Loop Until rFound.Address = FirstAddr
    Application.ScreenUpdating = True
  End If
End Sub
This helps me so much. Thank you for your keen insight. I have no further questions at this time.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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