So I have a merged cell that looks like below
Following is my code for putting a border around it:
Dim c As Range
For Each c In testing
If c.MergeCells Then
c.Interior.ColorIndex = 19
c.Borders.LineStyle = xlContinuous
c.Borders.Weight = xlThick
c.Borders.Color = vbGreen
End If
Next
This code only creates a border around the top left cell (see picture). How do I ensure that the border is placed around the entire merged cell?
Border can be applied by setting LineStyle and Color property of IBorders to the merged range.
VBA Border Property First, you need to specify the range or the cell where you wish to apply the border using the range object. After that, type a dot (.) and then select the “Borders” property from the list of properties and methods. Next, specify the border index from the contants avaiable.
Change Border Color Using Format Cells DialogSelect the cells with the borders requiring color change. Press Ctrl + 1 to launch the Format Cells dialog box or click the arrow with the border icon in the Home tab's Font Select More Borders… from the menu.
In VBA, there is a “MERGE” method that you can use to merge a range of cells or even multiple ranges into one. This method has an argument “Across” which is optional. If you specify TRUE it will merge each row in the range separately, and if you specify FALSE it will merge the entire range as one.
You have to use the MergeArea
of the referenced range.
Dim c As Range
For Each c In testing
If c.MergeCells Then
With c.MergeArea
.Interior.ColorIndex = 19
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThick
.Borders.Color = vbGreen
End With
End If
Next
Try
With c 'Range of the merged cell
.BorderAround , Weight:=xlMedium
.Borders.Color = vbGreen
End With
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With