Let's break down how to insert a shape, like a rectangle, using Excel VBA.
Understanding the Basics
- Shapes Collection: Excel stores all the shapes on a worksheet within the
Shapes
collection. You'll interact with this collection to add, modify, or delete shapes. - Shape Object: Each shape on the worksheet is represented by a
Shape
object. This object has properties (like position, size, color) and methods (like moving, resizing, adding text). - AddShape Method: The
AddShape
method of theShapes
collection is what you'll use to create a new shape.
Code Example (Inserting a Rectangle)
Sub InsertRectangle()
Dim shp As Shape 'Declare a variable to hold the shape object
'Insert a rectangle
Set shp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 100, 50, 150, 75)
'Explanation of the AddShape parameters:
' msoShapeRectangle: Specifies the shape type (rectangle). See below for other shape types.
' 100: Left position of the rectangle (in points).
' 50: Top position of the rectangle (in points).
' 150: Width of the rectangle (in points).
' 75: Height of the rectangle (in points).
'Optional: Customize the rectangle
With shp
.Fill.ForeColor.RGB = RGB(255, 0, 0) 'Set fill color to red
.Line.ForeColor.RGB = RGB(0, 0, 0) 'Set border color to black
.Line.Weight = 2 'Set border weight
.Name = "MyRectangle" 'Give the shape a name
.TextFrame.Characters.Text = "Hello!" 'Add text
.TextFrame.Characters.Font.Size = 12 'Set font size
End With
End Sub
Explanation of the Code
-
Sub InsertRectangle()
: This line starts the subroutine (macro). -
Dim shp As Shape
: This declares a variable namedshp
to store theShape
object. It's good practice to declare your variables. -
Set shp = ActiveSheet.Shapes.AddShape(...)
: This is the core line. It uses theAddShape
method to create the rectangle.ActiveSheet
: Refers to the currently active worksheet.Shapes
: Refers to the collection of shapes on the active sheet.AddShape
: The method to add a new shape.msoShapeRectangle
: Specifies that you want to create a rectangle.100, 50, 150, 75
: These are the coordinates and dimensions of the rectangle. They are in points. 72 points equal approximately one inch.
-
With shp ... End With
: This is aWith
block. It's a convenient way to work with the properties of theshp
object without having to repeatshp.
every time. -
.Fill.ForeColor.RGB = RGB(255, 0, 0)
: Sets the fill color to red.RGB(255, 0, 0)
represents red. You can use other RGB values for different colors. -
.Line.ForeColor.RGB = RGB(0, 0, 0)
: Sets the border color to black. -
.Line.Weight = 2
: Sets the border weight. -
.Name = "MyRectangle"
: Assigns the name "MyRectangle" to the shape. This makes it easier to refer to the shape later in your code. -
.TextFrame.Characters.Text = "Hello!"
: Adds the text "Hello!" to the rectangle. -
.TextFrame.Characters.Font.Size = 12
: Sets the font size of the text to 12 points. -
End Sub
: This line ends the subroutine.
Other Shape Types
You can change the shape type by using different mso
constants:
msoShapeOval
: OvalmsoShapeLine
: LinemsoShapeRoundedRectangle
: Rounded rectanglemsoShapeStar
: StarmsoShapeRightArrow
: Right arrow- And many more! You can find a full list by searching for "MsoShape constants" online.
How to Use the Code
- Open the VBA editor in Excel (Alt + F11).
- Insert a new module (Insert > Module).
- Paste the code into the module.
- Run the macro by pressing F5 or going to Run > Run Sub/UserForm.
This will insert a rectangle onto your active worksheet. You can modify the code to change the shape type, position, size, color, and other properties.
0 件のコメント:
コメントを投稿