Sub 選択した幅で画像挿入()
Dim PictName As String
Dim GetPic As Shape
Dim SelWidth As Long
'選択しているもがvbObjectなら処理を抜ける
If VarType(Selection) = vbObject Then Exit Sub
'選択列が26列を超えていたら終了
If Selection.Cells.Columns.Count > 26 Then
MsgBox "ページ幅を超えています。"
Exit Sub
End If
'選択行が43行を超えていたら終了
If Selection.Cells.Rows.Count > 43 Then
MsgBox "ページの高さを超えています。"
Exit Sub
End If
'選択したセルの個数が1118を超えていたら終了
If Selection.Cells.Count > 1118 Then
MsgBox "指定範囲が広すぎます。"
Exit Sub
End If
SelWidth = Selection.Width '選択した幅を記憶
' 画像取得ダイアログを開く
PictName = Application.GetOpenFilename _
("画像(*.png; *.jpg; *.jpeg;*.emf; *.gif),*.png; *.jpg;*.emf; *.jpeg; *.gif", , "画像ファイルの選択")
' ダイアログでキャンセルした場合は終了
If PictName = "False" Then Exit Sub
' 画像の取得
With ActiveSheet
Set GetPic = .Shapes.AddPicture( _
fileName:=PictName, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Left:=Selection.Cells.Left, _
Top:=Selection.Cells.Top, _
Width:=-1, _
Height:=-1)
End With
With GetPic
.LockAspectRatio = msoTrue
.Width = SelWidth
End With
End Sub