Changing the border color\thickness

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have the following SNIPPET of code: -

Set xla = CreateObject("Excel.Application")
Set xlw = xla.Workbooks.Open(strFilename1, ReadOnly:=False)
Set xls = xlw.Worksheets(1)

xls.Cells(lngRow, intCol).Font.Color = 0
xls.Cells(lngRow, intCol).Font.Name = "Trebuchet MS"

I am trying to change the color and thickness of particular cells. Note I haven't given all the code only the bit that's needed to solve the problem (hopefully). The values of lngRow and intCol is determined by other code. Thanks in advance.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Repeat after me: The macro recorder is my friend... ;)

Use it to find out the code it uses for changing the cell border width/color. If you go through the Format Cells dialog instead of using the toolbar button, just remember to press ok before you switch line thickness or else it will only record the last change you've selected.

(So give a cell a thin border, click ok. Go back, give the cell a thick border, click ok.)

Hope it helps,
 
Upvote 0
Here's just a quick example. This will make a thick border on the right hand side of the selected cell and the colour is yellow. I got the syntax by using the Macro Recorder (Tools|Macro|Record New Macro...) and I suggest you do the same to get the syntax for the particular border styles and colours you want. Eventually you can trim your code down to something simple like this:

Code:
Public Sub test()
  Dim oRange As Range
  
  Set oRange = Selection

  With oRange
    With .Borders(xlEdgeRight)
      .LineStyle = xlContinuous
      .Weight = xlThick
      .ColorIndex = 19
    End With
  End With
End Sub

...obviously, you should be able to adapt this code to using something simple like:

Code:
  With xls.Cells(lngRow, intCol)
    With .Borders(xlEdgeRight)
      .LineStyle = xlContinuous
      .Weight = xlThick
      .ColorIndex = 19
    End With
  End With

I hope this gets you started.
 
Upvote 0
Thanks guys for the quick response.

I will look at the macro recorder. I'm new to Excel and hence didn't know about it. Thanks for pointing it out. Would the macro recorder have helped me in this problem?
 
Upvote 0
azizrasul said:
Would the macro recorder have helped me in this problem?

In your particular case I would say "yes", because it's apparent that you have some decent VBA skills. All you were lacking was the syntax for changing the border colour and thickness.
 
Upvote 0
Well, it would have shown you the code for the LineStyle, Weight and colorindex as was posted above... :)
 
Upvote 0
OK. I get it. That's pretty useful. Thanks guys.
 
Upvote 0

Forum statistics

Threads
1,215,851
Messages
6,127,288
Members
449,373
Latest member
jesus_eca

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