Function help

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
708
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
I am trying to count the number of cells in a range that have a red border.
Tried different ways but something just isn't right. I can't get it to work.
Here is what I have.
Thanks for the help
PuJo
VBA Code:
Option Explicit


Public Function CountRedBorderedCells(MyRange As Range) As Integer

    '=CountRedBorderedCells(B11:H16) (formula I use in the cell)

    Dim MyBorderCount As Integer
    Dim c As Variant
    
    For Each c In MyRange
    
        If c.Borders(xlEdgeLeft).Borders.ColorIndex = 3 And _
           c.Borders(xlEdgeRight).Borders.ColorIndex = 3 And _
           c.Borders(xlEdgeTop).Borders.ColorIndex = 3 And _
           c.Borders(xlEdgeBottom).Borders.ColorIndex = 3 Then
            
            MyBorderCount = MyBorderCount + 1
        End If
    Next c
    
    CountRedBorderedCells = MyBorderCount
    
End Function
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Close, but I think your syntax is off. Try:
VBA Code:
Public Function CountRedBorderedCells(MyRange As Range) As Integer
    Dim MyBorderCount As Integer
    Dim c As Variant
    For Each c In MyRange
        If c.Borders(xlEdgeLeft).ColorIndex = 3 And _
           c.Borders(xlEdgeRight).ColorIndex = 3 And _
           c.Borders(xlEdgeTop).ColorIndex = 3 And _
           c.Borders(xlEdgeBottom).ColorIndex = 3 Then
            
            MyBorderCount = MyBorderCount + 1
        End If
    Next c
    CountRedBorderedCells = MyBorderCount
End Function
 
Upvote 0
You need to remove the Borders from infront of ColorIndex.
 
Upvote 0
However you can also simplify it slightly like
VBA Code:
        If c.Borders.ColorIndex = 3 Then
            MyBorderCount = MyBorderCount + 1
        End If
 
Upvote 0
I thought that I had to account for all sides of the cell as to not get the logic confised with another cell that only has one side bordered or something...
 
Upvote 0
No need to check all the edges, as that's what Borders does if you don't specify an edge.
 
Upvote 0

Forum statistics

Threads
1,215,417
Messages
6,124,783
Members
449,188
Latest member
Hoffk036

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