PDA

View Full Version : Time of creation of shape



ElCidCampead
01-23-2017, 04:06 AM
Hello,

my purpose is to create a sort of "Undo" button which delete a specific shape, in particular the last one I created.

My idea is to use this code:


Dim sh As Shape
For Each sh In ActiveSheet.Shapes
sh.Delete
Next sh

where instead of deleting all shapes I would delete only the last one I added.
If I find out the time of creation of the last shape, may I delete the shape added in a time range (in this case, now -> now -10 seconds, for example)?
Is there a command that show me the time of creation of shape and has shape as object a property which define the time of its creation? If yes, I could add an if cycle inside the for cycle in order to filter all the shapes added in the last 10 seconds and delete them.
Thanks

Kenneth Hobs
01-23-2017, 08:32 AM
Welcome to the forum!

Since there is no date/time property for shapes, you will have to use another method. One can store data to a sheet for such.

To just delete the last added shape on a sheet, the last added can be found. e.g

Sheet1.Shapes(Sheet1.Shapes.Count).Delete

ElCidCampead
01-23-2017, 08:39 AM
Welcome to the forum!

Since there is no date/time property for shapes, you will have to use another method. One can store data to a sheet for such.

To just delete the last added shape on a sheet, the last added can be found. e.g

Sheet1.Shapes(Sheet1.Shapes.Count).Delete

This is exactly what I was looking for: thank you man!

ElCidCampead
01-24-2017, 06:14 AM
Another problem.
I want to use the code suggested above with an if cycle: the idea is that if I delete a certain type of shape (e.g. line), my code do a particular action (like writing something).
Otherwhise, if I delete another type of shape (like textbox), my code will do another thing. Is it possible?
I don't understand how Excel would "understand" the difference: is there a particular command?
thanks

Paul_Hossler
01-24-2017, 07:52 AM
You'll need to check .Type and .AutoshapeType

Here's a simple example

The enumerations are in the two links




Option Explicit

Dim iCounter As Long

'http://bettersolutions.com/vba/enumerations/msoautoshapetype.htm
'http://bettersolutions.com/vba/enumerations/msoshapetype.htm

Sub DeleteLastShape()
With ActiveSheet
If .Shapes(.Shapes.Count) > 0 Then
With .Shapes(.Shapes.Count)

Select Case .Type

Case msoTextBox
iCounter = iCounter + 1
Cells(iCounter, 5).Value = "TextBox"

Case msoAutoShape
iCounter = iCounter + 1
Cells(iCounter, 5).Value = "Autoshape"

Select Case .AutoShapeType
Case msoShapeArc
iCounter = iCounter + 1
Cells(iCounter, 5).Value = "Arc"
Case msoShapeBalloon
iCounter = iCounter + 1
Cells(iCounter, 5).Value = "Balloon"
End Select

Case msoLine
iCounter = iCounter + 1
Cells(iCounter, 5).Value = "Line"
End Select
.Delete
End With
End If
End With

End Sub