Marco for making borders around 'groups'

Piet1990

New Member
Joined
Dec 24, 2023
Messages
2
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello,

Im quite new in making excel-marcos.
And Im trying to figure it out or its possible to make a marco that automatically places a border around each of my groups
The difficult part with this is that not all of my groups contain the same amount of rows. Some are only 1 row while others can be 10 rows.

See example below to make it more clear

How it is:
1703414647429.png


How I want it:
1703414686353.png




Thanks a lot!
Piet
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Please try the following on a copy of your workbook. Run the macro with the sheet active.
VBA Code:
Option Explicit
Sub test()
    Const LCol As Long = 9
    Dim BRow As Long, LRow As Long, i As Long
    Dim r As Range
    BRow = Cells.Find("*", , xlFormulas, , 1, 2).Row
    LRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = LRow To 1 Step -1
        If Cells(i, 1) <> "" Then
            Set r = Range(Cells(i, 1), Cells(LRow, LCol))
            r.BorderAround ColorIndex:=1, Weight:=xlMedium
        End If
    Next i
End Sub
 
Upvote 1
Solution
Please try the following on a copy of your workbook. Run the macro with the sheet active.
VBA Code:
Option Explicit
Sub test()
    Const LCol As Long = 9
    Dim BRow As Long, LRow As Long, i As Long
    Dim r As Range
    BRow = Cells.Find("*", , xlFormulas, , 1, 2).Row
    LRow = Cells(Rows.Count, 1).End(xlUp).Row
   
    For i = LRow To 1 Step -1
        If Cells(i, 1) <> "" Then
            Set r = Range(Cells(i, 1), Cells(LRow, LCol))
            r.BorderAround ColorIndex:=1, Weight:=xlMedium
        End If
    Next i
End Sub
It works.
Thanks a lot for the help!
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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