Let's explore how to manipulate the border (or line) of a shape using Excel VBA, covering thickness (weight), color, and line style.
Key Properties and Methods
Shape.Line
Property: This property returns aLineFormat
object, which is what you'll use to control the line's appearance.LineFormat.Weight
Property: Controls the thickness of the line (in points).LineFormat.ForeColor
Property: Controls the color of the line. You'll typically use theRGB
function to specify colors.LineFormat.DashStyle
Property: Controls the line style (solid, dashed, dotted, etc.).
Code Examples
Sub ModifyShapeBorder()
Dim shp As Shape
' Assuming you have a shape selected or know its name
' If you don't have a specific shape, you can add one like this:
' Set shp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 100, 50, 150, 75)
' For this example, let's assume you have a shape named "MyShape"
Set shp = ActiveSheet.Shapes("MyShape") ' Or select the shape manually
If Not shp Is Nothing Then ' Check if the shape exists
With shp.Line
' 1. Change the line weight (thickness)
.Weight = 2.5 ' Set to 2.5 points
' 2. Change the line color
.ForeColor.RGB = RGB(0, 0, 255) ' Set to blue
' 3. Change the line style (dash style)
.DashStyle = msoLineDash ' Dashed line
' Other options:
' msoLineSolid: Solid line (default)
' msoLineDashDot: Dash-dot line
' msoLineDashDotDot: Dash-dot-dot line
' msoLineCustom: Custom dash style (more advanced)
' 4. More Line Properties (Optional)
' .BeginArrowheadLength = msoArrowheadLengthMedium ' Arrowhead length
' .BeginArrowheadStyle = msoArrowheadStyleTriangle ' Arrowhead style
' .EndArrowheadLength = msoArrowheadLengthMedium
' .EndArrowheadStyle = msoArrowheadStyleNoArrow ' No arrow at the end
' .Style = msoLineSingle ' Line style (single, double, etc.)
End With
Else
MsgBox "Shape 'MyShape' not found."
End If
End Sub
Explanation
-
Dim shp As Shape
: Declares a variable to hold the shape object. -
Set shp = ActiveSheet.Shapes("MyShape")
: This line gets the shape object. Important: Replace"MyShape"
with the actual name of your shape. If you don't know the name, you can select the shape and look at its name in the Name Box (the box to the left of the formula bar). Alternatively, you can add a shape programmatically as shown in the commented-out lines. -
If Not shp Is Nothing Then
: This checks if the shape was found. It's good practice to include this check to avoid errors if the shape doesn't exist. -
With shp.Line
: ThisWith
block makes it easier to work with theLineFormat
properties. -
.Weight = 2.5
: Sets the line thickness to 2.5 points. -
.ForeColor.RGB = RGB(0, 0, 255)
: Sets the line color to blue.RGB(0, 0, 255)
represents blue (Red=0, Green=0, Blue=255). Adjust the RGB values for other colors. -
.DashStyle = msoLineDash
: Sets the line style to dashed. See the comments in the code for othermsoLine
constants. -
More Line Properties: The commented-out lines show how to control arrowheads and line styles (single, double, etc.). Uncomment and modify these as needed.
-
Else MsgBox ...
: Displays a message box if the shape is not found.
How to Use
- Open the VBA editor (Alt + F11).
- Insert a new module (Insert > Module).
- Paste the code into the module.
- Modify
"MyShape"
to the correct name of your shape. - Run the macro (F5 or Run > Run Sub/UserForm).
This will change the border of the specified shape. Remember to adjust the code to match the name of your shape and your desired border properties.
0 件のコメント:
コメントを投稿