vba 形状重绘

vba shape redraw

在Excel中,如果我将形状添加到sheet然后删除它,形状对象仍然保留在内存中。有没有办法重绘那个形状?请参阅下面的代码以供参考。

Sub DrawTest()
    Dim x As Excel.Shape

    Set x = ActiveSheet.Shapes.AddLine(100, 100, 200, 200)
    Debug.Print (x Is Nothing) & " " & ObjPtr(x) 'Returns:  False 39620608

    x.Delete
    Debug.Print (x Is Nothing) & " " & ObjPtr(x)  'Returns:  False 39620608

    '
    ' Can I redraw it, at this point??    '
    '

    Set x = Nothing
    Debug.Print (x Is Nothing) & " " & ObjPtr(x)  'Returns:  True 0

End Sub

微软帮助说明shape.delete方法是删除形状。 如果您想稍后使用该形状,那么 .visible =true/false 可能会有所帮助?

不,您不能重新绘制该形状。

此外,实际形状不会保留在内存中,但变量 x 仍在为该形状分配 space。

因此,x 仍然是 Not Nothing,并且直到您将它的引用设置为 nothing(就像您在最后一个 Debug.Print 语句之前所做的那样)

抱歉这个坏消息。