2016-12-19 6 views
2

次のコードを使用してスプレッドシートに画像を追加しようとしています。vbaは形状を追加し、3秒待ってから削除します

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が待機せず、画像を直ちに削除します。

私が間違っていることを誰かに見せてもらえますか?ありがとう

+0

すぐに問題を解決するには、1分に変更してみてください。それはまだ遅れていないのですか?そうでない場合は、コードを実行して( 'F8'を使用)、何らかの理由でその行がスキップされるかどうかを確認します。 – BruceWayne

+0

'TimeValue'ではなく' CDate( "00:00:03") 'を試してください。ちょうど推測。私はいつもCDateを使います。 –

+0

代わりに 'Application.Wait Now + TimeSerial(0、0、3)'を試してください。地域の設定によって影響を受けません。 – Comintern

答えて

3

ああ、Application.OnTimeは、呼び出すコードサブルーチンである2番目の引数をとります。

ですから、

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

Sub Sub2() 
    'delete pic 
End Sub 

を必要と私はあなたがApplication.Waitを使用したい知っているが、それは多分あなたは、メッセージキューに戻って得るために持っていること。

+0

@S Meadenこれはうまくいきましたが、私の写真は削除されません。エラーはなく、ただ削除しません。 .deleteは使用する正しい関数ですか? –

+0

既存のコードを使用して画像を削除しますが、モジュールレベルの変数に参照を保存します。 –

関連する問題