[VBA] How to Change the Outline Color of a Shape (AutoShape)

目次

Introduction

In this article, I will share how to change the outline (border) color of a shape using VBA.

I previously learned how to change the fill color, but I also wanted to customize the line color to suit my design. Here is the method I found.

Conclusion: Use “.Line.ForeColor.RGB”

You can specify the outline color using the property .Line.ForeColor.RGB.

Sample Code:

ActiveSheet.Shapes(1).Line.ForeColor.RGB = RGB(255, 255, 255)

This code sets the outline color of the first shape on the active sheet to White.

Note:

  • RGB(255, 255, 255) represents White.
  • If you want to set it to Black, use RGB(0, 0, 0).

Common RGB Color Codes

Here is a list of common colors and their RGB values for your reference.

Color NameRGB Code
BlackRGB(0, 0, 0)
WhiteRGB(255, 255, 255)
RedRGB(255, 0, 0)
GreenRGB(0, 255, 0)
BlueRGB(0, 0, 255)
GrayRGB(128, 128, 128)

Specifying a Shape by Name

If you have multiple shapes on a sheet, it is safer to specify the target shape by its name rather than its index number.

' Example: Changing the outline of a shape named "Rectangle 1" to Black
ActiveSheet.Shapes("Rectangle 1").Line.ForeColor.RGB = RGB(0, 0, 0)

Summary

  • To change the outline color, use Shape.Line.ForeColor.RGB = RGB(Red, Green, Blue).
  • Remember that RGB(255, 255, 255) is White and RGB(0, 0, 0) is Black.
  • You can access shapes by their index number (e.g., Shapes(1)) or by their name (e.g., Shapes("Name")).

Through this exercise, I deepened my understanding of basic shape manipulation in VBA. Next, I plan to explore how to change the weight (thickness) and style (dashed lines, etc.) of the borders.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次