如何创建一个以特定颜色添加形状并对其进行分组的宏?

时间:2018-01-16 09:04:32

标签: excel vba excel-vba userform

请帮忙吗?我正在尝试在Excel上创建一个按钮,一旦点击就会打开UserForm1(通过一个具有UserForm1.Show命令的宏)。

此后,我希望userform提供选项。然后这些选项将被选中(当选择时创建2个矩形形状并将它们分组。请查看下面的一些屏幕截图以及宏录制代码(太具体)。

终极目标:我想在Excel上创建贴图。我已经创建了2个块,并希望每次都将它们组合在一起。因此,每当我点击一个按钮时,它就会为我创造一个粘性:)

我得到的错误是

  

找不到具有指定名称的项目

表单上2个选项的代码:

Private Sub OptionButton1_Click()
 ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285, 74.25, 112.5, 108.75). _
        Select
    Selection.ShapeRange.Line.Visible = msoFalse
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent6
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0.6000000238
        .Transparency = 0
        .Solid
    End With
    ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285.75, 74.25, 111.75, 21.75). _
        Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent6
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0.400000006
        .Transparency = 0
        .Solid
    End With
    Selection.ShapeRange.Line.Visible = msoFalse
    Range("J11").Select
    ActiveSheet.Shapes.Range(Array("Rectangle 23")).Select
    ActiveSheet.Shapes.Range(Array("Rectangle 23", "Rectangle 24")).Select
    Selection.ShapeRange.Group.Select

End Sub

Private Sub OptionButton2_Click()
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 286.5, 74.25, 111, 108.75). _
        Select
    Selection.ShapeRange.Line.Visible = msoFalse
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent5
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0.400000006
        .Transparency = 0
        .Solid
    End With
   ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285.75, 74.25, 111.75, 18.75). _
        Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent5
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = -0.25
        .Transparency = 0
        .Solid
    End With
    ActiveSheet.Shapes.Range(Array("Rectangle21")).Select
    ActiveSheet.Shapes.Range(Array("Rectangle21", "Rectangle22")).Select
    Selection.ShapeRange.Group.Select
End Sub

code as image

用户表单和最终产品:

enter image description here

2 个答案:

答案 0 :(得分:2)

您应该使用变量404 Not found error<IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On RewriteBase / RewriteCond %{QUERY_STRING} ^(.+)$ RewriteRule ^(.*)$ $1%1? [R=301,L] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 来记住新添加的形状。请注意,您必须使用Shape1并且无法使用Shape2直接访问该项目(Excel有点奇怪)。

通过这种方式,您可以独立于硬编码的形状名称。

.OLEFormat.Object

答案 1 :(得分:0)

感谢所有帮助,我稍微调整了一下代码并自行解决了。基本上,我必须确保我可以正确地获取对象的名称以对它们进行分组。看看下面的代码:

Private Sub OptionButton1_Click()
Dim James1 As Shape
Dim James2 As Shape

Set James1= ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285, 74.25, 112.5, 108.75)
James1.Select
Selection.ShapeRange.Line.Visible = msoFalse
With Selection.ShapeRange.Fill
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorAccent6
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = 0.6000000238
    .Transparency = 0
    .Solid
End With
  Set James2= ActiveSheet.Shapes.AddShape(msoShapeRectangle, 285.75, 74.25, 111.75, 21.75)
  James2.Select
    With Selection.ShapeRange.Fill
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorAccent6
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = 0.400000006
    .Transparency = 0
    .Solid
    End With
    Selection.ShapeRange.Line.Visible = msoFalse

    Dim S1 As String
    Dim S2 As String

    S1 = James1.Name
    S2 = James2.Name

  ActiveSheet.Shapes.Range(Array(S1, S2)).Select
    Selection.ShapeRange.Group.Select