Add a bottom border to last merge row in this VB Code?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
921
Office Version
  1. 365
  2. 2019
The code below merges a number of cells in column A and V. How would I also have it add a bottom border to the last row in each cell merge?

For instance, it may merge A3:A9 and V3:V9. I would want a bottom border added to A9 through V9. Also, how would I specify the color of the border?

Thanks


On Error GoTo safeExit
headerRow = 1
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
f = headerRow + 1
Application.DisplayAlerts = False
Do Until f > lastRow
l = f + 1
Do Until (Cells(l, 1) <> Cells(l - 1, 1)) Or (Cells(l, 22) <> Cells(l - 1, 22))
l = l + 1
Loop
If f <> l Then
For c = 1 To 22 Step 21
With Range(Cells(f, c), Cells(l - 1, c))
.merge
.VerticalAlignment = xlCenter
End With
Next c
End If
f = l
Loop
safeExit:
Application.DisplayAlerts = True
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Does this do it? Or at least get you started in the right direction?

Rich (BB code):
                With Range(Cells(f, c), Cells(l - 1, c))
                    .Merge
                    .VerticalAlignment = xlCenter
                    .Borders(xlEdgeBottom).Weight = xlMedium
                    .Borders(xlEdgeBottom).ColorIndex = 3
                End With
 
Upvote 0
I added that after Application.DisplayAlerts = True

Isn't applying a green border anywhere though :(
 
Upvote 0
It wasn't really meant to go there. I included the "snippet" of your original code and highlighted what/where I added. The "3" in that code will make a red border. I think "50" will give you a green... play with the numbers to get the colors you want.
 
Last edited:
Upvote 0
It wasn't really meant to go there. I included the "snippet" of your original code and highlighted what/where I added. The "3" in that code will make a red border. I think "50" will give you a green... play with the numbers to get the colors you want.

Oops! Sorry, that was pretty obvious if I'd just slowed down.

Red it is - I tried looking up the codes online and on what I was looking at 3 was green? :)

Not sure if I didn't explain it well though. That does indeed highlight the bottom of the last cell in the merge, but only in columns A and V. I need the same bottom border on all the cells in between as well.
 
Upvote 0
Oops! Sorry, that was pretty obvious if I'd just slowed down.

Red it is - I tried looking up the codes online and on what I was looking at 3 was green? :)

Not sure if I didn't explain it well though. That does indeed highlight the bottom of the last cell in the merge, but only in columns A and V. I need the same bottom border on all the cells in between as well.

Didn't mean highlight - meant that it does indeed add a border to the bottom of the last cell in the merge....
 
Upvote 0
I'm struggling to grasp what/where you code is acting. This is a stab in the dark.... hopefully if wrong enough to spark your imagination on how to do it in the correct spot...

Code:
        If f <> l Then
            For c = 1 To 22 Step 21
                With Range(Cells(f, c), Cells(l - 1, c))
                    .Merge
                    .VerticalAlignment = xlCenter
                End With
            Next c
[COLOR="Red"]            With .Range(Cells(l - 1, 1), Cells(l - 1, 22))
                .Borders(xlEdgeBottom).Weight = xlMedium
                .Borders(xlEdgeBottom).ColorIndex = 50
            End With
[/COLOR]        End If
 
Upvote 0
Had to change "With .Range" to "With Range", but That is almost it!

It's adding the border correctly where needed across the entire row with one exception.

The only issue is that its adding a border to the bottom of B2 through U2 (not column A or V though) which should not be there. In my spreadsheet A2:A3 and V2:V3 are merged, so the first border on the spreadsheet should be at the bottom of A3 through V3.

Again, row 1 is the header row (and I can manually add a border to the bottom of that row, so no need for the macro to add it).

Thanks
 
Upvote 0
Had to change "With .Range" to "With Range", but That is almost it!

It's adding the border correctly where needed across the entire row with one exception.

The only issue is that its adding a border to the bottom of B2 through U2 (not column A or V though) which should not be there. In my spreadsheet A2:A3 and V2:V3 are merged, so the first border on the spreadsheet should be at the bottom of A3 through V3.

Again, row 1 is the header row (and I can manually add a border to the bottom of that row, so no need for the macro to add it).

Thanks

UPDATE:

When I ran it again with some different data, it added the extra border in B through U in a few different places......

PM me your e-mail address if you'd like me to send you the actual spreadsheet. Thanks
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

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