Create border around cells with different values using either VBA or conditional formatting

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
In column AF, I have numbers or strings (the cells are formatted as text) 1 or 2.

It follows like this:
1
2
1
2
......

So for the first set, from Col B to Col AE (Excluding Col U and V), I want to compare the values for the corresponding 1 and 2 rows.

If they are different, then I want a border around those cells.

For example
B4 = Cat, ......, AF4 = 1
B5 = Cats, ....., AF5 = 2

the above represent "a set" as described before.

And in this case, I want a border around B4:B5.

Then I check all other "twin cells" as explained earlier.

How do I achieve that?

Thanks in advance.
Kelly.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi Kelly Mort,

Here is a Conditional Format approach.

The formula
Excel Formula:
=AND($AF4="1",OR(OFFSET($B4,,,,19)<>OFFSET($B4,1,,,19),OFFSET($W4,,,,9)<>OFFSET($W4,1,,,9)))
builds the left, top, right line for the box.
the formula
Excel Formula:
=AND($AF4="2",OR(OFFSET($B4,,,,19)<>OFFSET($B4,-1,,,19),OFFSET($W4,,,,9)<>OFFSET($W4,-1,,,9)))
builds the left, bottom, right lines for the box

Columns G to AD are hidden but all contain nulls.


Kelly Mort.xlsx
ABCDEFAEAFAG
3
4Cats1
5Cat2
6Dog1
7Dog2
8Sheep1
9SheepPig2
10GoatCow1
11GoatCow2
12HorsePig1
13HorsePig2
14
15
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B4:B17Expression=AND($AF4="2",OR(OFFSET($B4,,,,19)<>OFFSET($B4,-1,,,19),OFFSET($W4,,,,9)<>OFFSET($W4,-1,,,9)))textNO
B4:B20Expression=AND($AF4="1",OR(OFFSET($B4,,,,19)<>OFFSET($B4,1,,,19),OFFSET($W4,,,,9)<>OFFSET($W4,1,,,9)))textNO
 
Upvote 0
@Toadstool , where should I place the formula?

When I enter it through the "use a formula to determine which cells to format" rule, it throws an error message at me saying there is a problem with the formula.
 
Upvote 0
@Toadstool , where should I place the formula?

When I enter it through the "use a formula to determine which cells to format" rule, it throws an error message at me saying there is a problem with the formula.
You need to use that little icon to the right of each formula "Copy to clipboard" to get the formula, then select cells B4 and down as far as you want.
Use New Rule or Manage Rules and "Use a formula to determine which cells to format" and in "Format values where this formula is true" paste in the formula from the clipboard.
For format select Borders and the left, top and right. Repeat these steps with B4 down still selected but use th 2nd formula and a border format of left, bottom, right.
NOTE: You said column AF contained a 1 or a 2 in text. If it's actually numeric then just change that "1" and "2" in the formulae to 1 and 2.

1625182428393.png
 
Upvote 0
Okay it's working now.

But it does not create the borders for the other columns C:AE excluding U and V.

When C4 and C5 have different values, the rule should set borders around them and same should apply throughout to AE excluding U and V..
 
Upvote 0
Hi Kelly,

The following does the job for each pair of rows in the tab 'ws' - it does not look at the 1 or 2 flag and case sensitivity is also ignored so not sure with those caveats the following provides a suitable solution:

VBA Code:
Option Explicit
Sub KellyMort()

    Dim ws As Worksheet
    Dim i As Long, j As Long, x As Long
    Dim r As Range
    
    Application.ScreenUpdating = False
    
    Set ws = ThisWorkbook.Sheets("Sheet1") 'Sheet containing the data. Change to suit if necessary.
    
    On Error Resume Next 'Ignore error if there's no data in 'ws'
        j = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        If j < 4 Then
            MsgBox "The expected first data row is four." & vbNewLine & "Please check """ & ws.Name & """ and try again.", vbExclamation
            Exit Sub
        End If
    On Error GoTo 0
    
    For i = 4 To j Step 2
        For x = 2 To 31 'Columns B to AE
            If x <> 21 Or x <> 22 Then 'Ignore columns U and V
                Set r = Range(ws.Cells(i, x), ws.Cells(i + 1, x))
                If StrConv(ws.Cells(i, x), vbUpperCase) = StrConv(ws.Cells(i + 1, x), vbUpperCase) Then
                    r.Borders.LineStyle = xlLineStyleNone
                Else
                    r.BorderAround Weight:=xlThin
                End If
            End If
        Next x
    Next i
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 0
Solution
Kelly Mort, I am pleased you have a found a solution.
Just to answer your last question, you can change the Conditional Format "Applies to" to extend which cells get the borders.
1625217212663.png
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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