エクセル VBA 画像の貼り付け

ネットで,いろいろあさっていると,VBAで,エクセルに写真を貼り付けるものがめにはいりました。

のようにセルに画像を貼り付けるVBAです。
C列にある名前をもとに,ホルダーに「名前.jpg」のファイルを検索して,A列に貼り付けるものです。
世の中には,本当にすごい方がいるもんだとつくづく思います。

Sub temp()
Dim c As Range, i As Long, img As String

'画像フォルダーのパス
Const imgFolderPath = "C:\Users\mabo-2021\Desktop\ブログー移行\tesut\"

For i = 2 To Cells(Rows.Count, 3).End(xlUp).Row
    Set c = Cells(i, 1)
    img = imgFolderPath & c.Offset(, 2).Text & ".jpg"
    
    MsgBox img
    MsgBox Dir(img)

    If Dir(img) <> "" Then
        With ActiveSheet.Shapes.AddPicture(img, msoFalse, msoTrue, c.Left, c.Top, -1, -1) '//-1→元のサイズ
        'expression. AddPicture(Filename ,LinkToFile,SaveWithDocument,Left,Top,Width,Height)
            .LockAspectRatio = True
            .Placement = xlMove
            .Height = c.Height
            If .Width > c.Width Then
                .Width = c.Width
                .Top = c.Top + (c.Height - .Height) / 2
            End If
        End With
    Else
        c.Value = "No Image"
    End If
Next i
End Sub

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA