简体   繁体   English

基于单元格值绘制形状

[英]Draw Shape Based on Cell Values

All, 所有,

I have code that creates a shape based on based on inputted values in the macro itself. 我有基于宏本身输入值创建形状的代码。 I am wanting to have the values of shape type, width, and height specified by the user (location of shape on the sheet is indifferent to me right now). 我想要用户指定的形状类型,宽度和高度的值(工作表上的形状位置现在对我无动于衷)。 The user would input the aforementioned numerical values for width and height into the cells and click a button which would output the shape type and size the user wants. 用户将上述宽度和高度的数值输入到单元格中,并单击一个按钮,该按钮将输出用户想要的形状类型和尺寸。

In my case, there will be a drop down box for "rectangle" and "circle". 在我的情况下,将有一个“矩形”和“圆圈”的下拉框。 I don't know how to get the code to read those words and convert it '1' and '9', respectively. 我不知道如何让代码读取这些单词并分别将其转换为“1”和“9”。 I may just have the user choose 1 or 9 to create the shape. 我可能只是让用户选择1或9来创建形状。

I would also like to add text to the center of the shape. 我还想将文字添加到形状的中心。 Again, I have created a code for this but it is within the macro. 我再次为此创建了一个代码,但它在宏中。 I would like to have the code reference a cell value instead. 我想让代码引用一个单元格值。 I assume it would be the same as above. 我假设它与上面相同。

Thank you for any assistance. 谢谢你的帮助。

Sub AddShape()

Dim s As Shape
Dim ws As Worksheet
Set ws = Sheets("Deck Layout")

'add a shape
Set s = ws.Shapes.AddShape(1, 80, 80, 75, 75)

'make it nearly white
s.Fill.ForeColor.RGB = RGB(245, 245, 255)

'show text within it
s.TextFrame.Characters.Text = "1"
s.TextFrame.Characters.Font.ColorIndex = 2

With s.TextFrame.Characters(0, 0)
s.TextFrame.HorizontalAlignment = xlHAlignCenter
s.TextFrame.VerticalAlignment = xlVAlignCenter
.Font.Color = RGB(0, 0, 0)

End With
End Sub

Since you've already got parts of the answer in the comments, I'll focus on the shape picking. 由于你已经在评论中得到了部分答案,我将专注于形状选择。
Have a look at this: 看看这个:

Dim ShapeType As MsoAutoShapeType

Select Case LCase(ws.Range("b1").Value)
    Case "rectangle"
        ShapeType = msoShapeRectangle
    Case "circle"
        ShapeType = msoShapeOval
End Select

Set s = ws.Shapes.AddShape(ShapeType, 80, 80, 75, 75)

It will find the value in B1, convert it to lower case and the test it for "rectangle" and "circle" and the set the ShapeType to a corresponding value. 它将在B1中找到值,将其转换为小写,并将其测试为“矩形”和“圆”,并将ShapeType设置为相应的值。
You can use 1 and 9 instead, but that is bad practice. 您可以使用1和9代替,但这是不好的做法。 Use the defined constants - it will make your code much easier to read. 使用定义的常量 - 它将使您的代码更容易阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM