vba添加形状,等待3秒然后删除

时间:2016-12-19 20:34:36

标签: excel image vba

我正在尝试使用以下代码将图像添加到电子表格中。

    Sub InsertPic()

    Dim pic As String 'file path of pic
    Dim myPicture As Picture 'embedded pic
    Dim rng As Range 'range over which we will iterate
    Dim cl As Range 'iterator

    Set rng = Range("C5")  '<~~ Modify this range as needed. Assumes image link URL in column A.
    For Each cl In rng
    pic = "https://openclipart.org/image/2400px/svg_to_png/167549/Kliponious-green-tick.png"

        Set myPicture = ActiveSheet.Pictures.Insert(pic)
        '
        'you can play with this to manipulate the size & position of the picture.
        ' currently this shrinks the picture to fit inside the cell.
        With myPicture
            .ShapeRange.LockAspectRatio = msoFalse
            .Width = 40
            .Height = cl.Height
            .Top = Rows(cl.row).Top - 2
            .Left = Columns(cl.Column).Left + 10
        End With
        '

     Next

   Application.Wait (Now + TimeValue("00:00:03"))

With myPicture
.delete
End With

     End Sub

延迟3秒后,我想删除图像。

出于某种原因,vba不会等待并立即删除图像。

请有人能告诉我我做错了什么吗?感谢

1 个答案:

答案 0 :(得分:3)

啊,Application.OnTime接受第二个参数,即要调用的代码子程序。

所以你需要

Sub Sub1()
 'Insert pic
  Application.OnTime now()+CDate("00:00:03"), "Sub2"
End Sub

Sub Sub2()
  'delete pic
End Sub

我知道你想使用Application.Wait,但也许你必须回到消息队列。