![](https://excel-vba.work/wp-content/themes/cocoon-master/images/b-woman.png)
VBAで
・画像ファイルを挿入
できます!
VBAコード
ここでは例として、
・デスクトップ配下の画像ファイル「サンプル画像.jpg」を
・シート「sample」のセル「B2」の位置へ挿入
します。
![画像ファイル「サンプル画像.jpg」](https://excel-vba.work/wp-content/uploads/2022/08/4b2ea9a625016092e1f26e9c006aae02.jpg)
Option Explicit
Sub sample()
Dim filePath As String
Dim ws As Worksheet
Dim topPosition As Double
Dim leftPosition As Double
Dim shapeObj As Shape
'画像ファイルを指定
filePath = "C:\Users\user\Desktop\サンプル画像.jpg"
'挿入先のシートを指定
Set ws = Worksheets("sample")
'挿入する位置を取得 ※セル「B2」の位置を取得
With ws.Range("B2")
topPosition = .Top
leftPosition = .Left
End With
'挿入する画像を取得
Set shapeObj = ws.Shapes.AddPicture( _
Filename:=filePath, _
LinkToFile:=msoTrue, _
SaveWithDocument:=msoTrue, _
Top:=topPosition, _
Left:=leftPosition, _
Width:=0, _
Height:=0 _
)
'画像を挿入
With shapeObj
.ScaleHeight 1, msoTrue
.ScaleWidth 1, msoTrue
End With
End Sub
実行結果
画像ファイルを挿入できました。
![実行結果](https://excel-vba.work/wp-content/uploads/2022/08/vba_image_insert_001.png)
参考
上記のVBAコードで使用した以下の詳細は、公式サイトをご確認ください。
●「Worksheetオブジェクト」の「Shapes」プロパティ
●「Shapesコレクション(オブジェクト)」の「AddPicture」メソッド
●「Shapeオブジェクト」の「ScaleHeight」メソッドと「ScaleWidth」メソッド