Error With VBA "Borderaround"

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to simply put a border around a cell. I'm not sure if this is the right way to go about doing it, but this is the line of code I'm using:

Code:
.Cells(dcomprow, 2).borderaround _
                Linestyle:=xlcontinuous, Weight:=xlThin, colorindex = 3

I get an error in the VBA compilor with "colorindex" ... Expected: named parameter.

What must I do to correct the error? I would like to use an RGB value instead of a color index. How would that be done?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
How about:

VBA Code:
    Dim rng As Range
    Set rng = Cells(dcomprow, 2)
'
    With rng.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .Color = RGB(255, 0, 0)
    End With
 
Upvote 0
I would like to use an RGB value instead of a color index. How would that be done?
Combining bits of both previous answers:
VBA Code:
.Cells(dcomprow, 2).BorderAround LineStyle:=xlContinuous, Weight:=xlThin, Color:=RGB(255, 0, 0)
 
Upvote 0
Solution
Thank you all for your contributions.

Can anyone explain why when this line is executed ...
Code:
.Range("B" & dcomprow).BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, Color:=RGB(255, 0, 0)

Only three of the four sides of the cell are receiving the border? This seems to be common with cells when the adjoining cell has a border. If the adjacent cell has no border, it takes the red border.
For example, the range being "B4", "C4" has a black border around it. The red order does not occupy the line between B4 and C4. B3 has no border, so the line between B3 and B4 takes the red border.
 
Upvote 0
which border has priority ? if the other one is xltick and this one is xmedium, that one wins ???
 
Upvote 0
The red border (BorderAround) is xlmedium. The adjacent cells are xlThin. I assume then, that the red BorderAround would take priority if priority is based on line weight.
 
Upvote 0
Seems that I have found the solution. I didn't have the issue once the line weight was changed to xlMedium.
Thanks again all.
 
Upvote 0
Just a comment: It may not be relevant to what you are doing but your question was about "BorderAround" and yet the marked solution would apply borders to all vertical/horizontal borders around or within the relevant range. So if it was being applied to a range of more than one cell, you would get more than just BorderAround.
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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