|
▼VBA勉強中 さん:
まだ、ちょっとわかりにくいところもありますがたたき台。
★のところ、シート名とフォルダは実際のものにしてください。
Sub Test()
Dim Pos As Range
Dim fPath As String
Dim fName As String
Dim Target As Range
Dim dic As Object
With Sheets("Sheet1") '★対象シート
.Pictures.Delete
Set Pos = Sheets("Sheet1").Range("D5")
End With
Set dic = CreateObject("Scripting.Dictionary")
fPath = CreateObject("WScript.Shell").SpecialFolders("DeskTop") & "\画像フォルダ\" '★
Do While Not IsEmpty(Pos)
fName = Right(Pos.Value, 3) & Pos.Offset(2).Value & ".jpg"
fName = Dir(fPath & fName)
If fName <> "" Then
If Not dic.exists(fName) Then
dic(fName) = True
Set Target = Pos.Offset(, 1)
With ActiveSheet.Shapes.AddPicture(Filename:=fPath & fName, LinkToFile:=False, _
SaveWithDocument:=True, Left:=Target.Left, Top:=Target.Top, _
Width:=-1, Height:=-1) '-1 元の大きさで貼り付け
'===============タテヨコの縮尺を保持して拡大または縮小
.LockAspectRatio = True '縦横比率の維持(念のため)
.Width = Target.Width * 0.9
If .Height > Target.Height * 0.9 Then .Height = Target.Height * 0.9
'===============中央へ調整
.Top = Target.Top + Target.Height / 2 - .Height / 2
.Left = Target.Left + Target.Width / 2 - .Width / 2
End With
End If
End If
Set Pos = Pos.Offset(, 11)
Loop
End Sub
|
|