dポイントプレゼントキャンペーン実施中!

みこむ。



マクロで「セルに入れたファイル名の画像を隣のセルに読み込む」作業をVBA
でつくりました。
そのファイル名がないときは、飛ばすようにできないでしょうか。
「 Set myPic = ActiveSheet.Pictures.Insert(sCurDir & myCell.Value & ".JPG")」
ここでとめられてしまいます。


   A(No)  B(名)    C(画像)
---------------------------------------------
1   1   test01   D:\画像\teet01.JPG
2   2   test02   D:\画像\teet02.JPG
3   3   test03   D:\画像\teet03.JPG



Private Sub CommandButton1_Click()
Dim i As Long
Dim myPic As Object
Dim myCell As Range
Dim sCurDir As String
sCurDir = ThisWorkbook.Path & "\画像\"


For i = 6 To Range("B" & ActiveSheet.Rows.Count).End(xlUp).Row Step 6
Set myCell = Range("B" & i)
Set myPic = ActiveSheet.Pictures.Insert(sCurDir & myCell.Value & ".JPG")
With myPic
.Left = Range("C" & i).Left
.Top = Range("C" & i).Top
.Width = Range("C" & i).MergeArea.Width
.Height = Range("C" & i).MergeArea.Height
End With
Set myPic = Nothing
Next i
End Sub

A 回答 (1件)

>そのファイル名がないときは、飛ばすようにできないでしょうか。



その通りにします。
if dir(scurdir & mycell.value & ".jpg") <> "" then
 Set myPic = ActiveSheet.Pictures.Insert(sCurDir & myCell.Value & ".JPG")
 With myPic
 .Left = Range("C" & i).Left
 .Top = Range("C" & i).Top
 .Width = Range("C" & i).MergeArea.Width
 .Height = Range("C" & i).MergeArea.Height
 End With
 Set myPic = Nothing
end if

この回答への補足

できました!ありがとうございます。

本来の目的以外なんですが、
これをjpgだけでなく png,gif,BMP,なども対応にすることは可能でしょうか?

補足日時:2013/08/13 11:01
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!