[Excel VBA] How to Change Shape Outline Weight, Color, and Style

目次

Introduction

In Excel document creation, formatting “Outlines” is essential for emphasizing shapes and creating a unified design. If you can manipulate these styles using VBA, you can automate report formatting and significantly improve efficiency.

In this article, I will explain how to show or hide outlines and change their weight, color, and line style (such as solid or dashed lines) using specific code examples.

Preparation: Naming the Target Shape

To manipulate a specific shape accurately with code, let’s name the shape “OutlineSample” in advance.

(How to name: Select Shape -> “Shape Format” tab -> “Selection Pane” -> Rename)

The “Line” Object for Manipulating Outlines

All settings related to shape outlines are managed by the properties of the Line object within the Shape object. The main properties are as follows:

  • Visible: Whether to show the outline (msoTrue to show, msoFalse to hide).
  • Weight: The thickness of the outline (Unit: Points).
  • ForeColor: Settings related to the outline color (specified by RGB value or Theme Color).
  • DashStyle: The type of line (Solid, Dash, Dash-Dot, etc.).

VBA Code Example: Setting Outline Formatting in Bulk

The following code sets the outline to be visible, and defines its weight, color, and style for the shape named “OutlineSample”.

Sub FormatShapeOutline()

    ' Target the shape named "OutlineSample"
    With ActiveSheet.Shapes("OutlineSample").Line
    
        ' Show the outline
        .Visible = msoTrue
        
        ' Set outline thickness to 3 points
        .Weight = 3
        
        ' Set outline color using RGB value (Steel Blue)
        .ForeColor.RGB = RGB(70, 130, 180)
        
        ' Set outline style to Dash
        .DashStyle = msoLineDash
        
    End With

End Sub

Explanation of the Code

By writing With ActiveSheet.Shapes("OutlineSample").Line, you can manipulate each property of the Line object using short descriptions until the End With statement.

  • .Visible = msoTrue: This setting shows the outline. If you want to hide it, specify msoFalse.
  • .Weight = 3: Sets the outline thickness to 3 points. Increasing this number makes the line thicker, while decreasing it makes it thinner.
  • .ForeColor.RGB = RGB(…): Uses the RGB property of the ForeColor object to specify the color.
  • .DashStyle = msoLineDash: Sets the line type to a dash. Other styles such as msoLineSolid (Solid) and msoLineDashDot (Dash-Dot) are available.

Advanced: Setting Outline Color using “Theme Colors”

Just like fill colors, you can link outline colors to Excel “Themes.” This allows you to create highly maintainable macros that adapt to workbook-wide design changes.

Sub SetOutlineColorByTheme()

    With ActiveSheet.Shapes("OutlineSample").Line.ForeColor
    
        ' Base color on the theme's "Accent 3"
        .ObjectThemeColor = msoThemeColorAccent3
        
        ' Make the base color 20% lighter
        .TintAndShade = 0.2
        
    End With

End Sub

Summary

In this article, I explained how to manipulate shape outlines using Excel VBA.

Key Points:

  • Outline settings are handled via the Shape.Line object.
  • Switch visibility using .Visible.
  • Specify thickness in points using .Weight.
  • Specify color with .ForeColor and line type with .DashStyle.

By mastering these properties, you can automatically create visually clear and well-designed documents, rather than just placing simple shapes. Please try using this in your daily tasks.

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

この記事を書いた人

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

目次