Adding Think Border Around Rows With Same Cell Value

Dratchsky

New Member
Joined
Apr 24, 2021
Messages
11
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Web
Hello,

This is my first post here. I was sure I would be able to find what I was looking for without posting however I didn't.

I would like to add a thick border around rows that share a common value

I have Column B titled Group ID and I would like to have a thick border placed around all the rows that share the same group ID

Below is a photo of the outcome I am looking for

1619273196747.png
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try:
VBA Code:
Sub InsertBorder()
    Application.ScreenUpdating = False
    Dim lRow As Long, i As Long, v As Variant, lVisRow As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("B2", Range("B" & Rows.Count).End(xlUp)).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v, 1)
            If Not .Exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Range("A1").CurrentRegion.AutoFilter 2, v(i, 1)
                lVisRow = Cells(Rows.Count, "B").End(xlUp).Row
                Range("A1").AutoFilter
                Rows(lVisRow).Borders(xlEdgeBottom).Weight = xlMedium
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you so much!
Try:
VBA Code:
Sub InsertBorder()
    Application.ScreenUpdating = False
    Dim lRow As Long, i As Long, v As Variant, lVisRow As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("B2", Range("B" & Rows.Count).End(xlUp)).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v, 1)
            If Not .Exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Range("A1").CurrentRegion.AutoFilter 2, v(i, 1)
                lVisRow = Cells(Rows.Count, "B").End(xlUp).Row
                Range("A1").AutoFilter
                Rows(lVisRow).Borders(xlEdgeBottom).Weight = xlMedium
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
Works like a gem!
 
Upvote 0
Here is another more compact way to write this macro...
VBA Code:
Sub InsertBorder()
  Dim Cell As Range
  Application.ScreenUpdating = False
  For Each Cell In Range("B2", Cells(Rows.Count, "B").End(xlUp))
    If Cell.Value <> Cell.Offset(1).Value Then Cell.EntireRow.Borders(xlEdgeBottom).Weight = xlMedium
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
VBA Code:
Sub ApplyThickBordersBasedOnGroupID()
    Dim ws As Worksheet
    Set ws = ActiveSheet ' Set this to the appropriate worksheet, e.g., Sheets("Sheet1")
    
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    
    Dim startRow As Long
    startRow = 2 ' Assuming your data starts at row 2
    
    Dim currentGroupID As Variant
    currentGroupID = ws.Cells(startRow, 2).Value
    
    Dim i As Long
    For i = startRow + 1 To lastRow + 1 ' Loop through each row, +1 to handle the last group
        If ws.Cells(i, 2).Value <> currentGroupID Or i = lastRow + 1 Then
            ' Apply border to the range from startRow to i-1
            With ws.Range(ws.Cells(startRow, 1), ws.Cells(i - 1, ws.Columns.Count)).Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Weight = xlThick
            End With
            With ws.Range(ws.Cells(startRow, 1), ws.Cells(i - 1, ws.Columns.Count)).Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlThick
            End With
            With ws.Range(ws.Cells(startRow, 1), ws.Cells(i - 1, ws.Columns.Count)).Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .Weight = xlThick
            End With
            With ws.Range(ws.Cells(startRow, 1), ws.Cells(i - 1, ws.Columns.Count)).Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Weight = xlThick
            End With
            
            ' Reset the start row and current Group ID
            startRow = i
            currentGroupID = ws.Cells(i, 2).Value
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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