为什么我在excel中出现此错误?

时间:2016-12-09 16:21:30

标签: excel vba

Sub Bubble2()

    ActiveSheet.Shapes.AddShape(msoShapeCloudCallout, 795, 8.25, 107.25, 41.25). _
        Select

    Selection.Name = "zooky"
    Selection.ShapeRange.Adjustments.Item(1) = -0.25029
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = "text.................."
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 10). _
        ParagraphFormat
        .FirstLineIndent = 0
        .Alignment = msoAlignLeft
    End With
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 10).Font
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.ObjectThemeColor = msoThemeColorLight1
        .Fill.ForeColor.TintAndShade = 0
        .Fill.ForeColor.Brightness = 0
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .Name = "+mn-lt"
    End With
    Range("P5").Select
End Sub

Sub FlipFlop2()
    With ActiveSheet.Shapes("zooky")
        .Visible = Not .Visible
    End With
End Sub

Excel告诉我此行没有带该名称的元素:使用ActiveSheet.Shapes(“zooky”)

我想要做的是,如果我点击我的元素excel应该绘制一个云形状,当我再次点击该元素时,它应该再次删除相同的云

任何想法?

1 个答案:

答案 0 :(得分:1)

最好避免使用SelectSelection,而应该为您创建的Shape定义Shape ObjectSet,(请参阅下面的代码)。

Option Explicit

Public Shp As Shape

Sub Bubble2()

    ' set object reference to new created Shape
    Set Shp = ActiveSheet.Shapes.AddShape(msoShapeCloudCallout, 795, 8.25, 107.25, 41.25)

    With Shp
        .Name = "zooky"
        .Adjustments.Item(1) = -0.25029
        .TextFrame2.TextRange.Characters.Text = "text.................."
    End With

    With Shp.TextFrame2.TextRange.Characters(1, 10)
        With .ParagraphFormat
            .FirstLineIndent = 0
            .Alignment = msoAlignLeft
        End With
        With .Font
            .NameComplexScript = "+mn-cs"
            .NameFarEast = "+mn-ea"
            .Fill.Visible = msoTrue
            .Fill.ForeColor.ObjectThemeColor = msoThemeColorLight1
            .Fill.ForeColor.TintAndShade = 0
            .Fill.ForeColor.Brightness = 0
            .Fill.Transparency = 0
            .Fill.Solid
            .Size = 11
            .Name = "+mn-lt"
        End With
    End With

    Range("P5").Select

    ' just for testing
    'Call FlipFlop2

End Sub

Sub FlipFlop2()
    With Shp
        .Visible = Not .Visible
    End With
End Sub