엑셀, 이미지를 한번에 넣자(VBA)

엑셀퀘스트 스터디클럽 · 데이터위자드
엑셀, 이미지를 한번에 넣자(VBA) 엑셀에서 여러 이미지를 셀에 맞춰 자동으로 넣는 작업은 반복되면 꽤 번거롭습니다.
이번에는 시작 셀만 지정하면 폴더 안의 이미지들을 순서대로 불러와 셀 크기에 맞게 배치해 주는 VBA 코드를 소개합니다.
반복 작업을 단순화하려는 분들에게 바로 적용 가능한 구조이며,

* 이미지는 파일명 가나다 순으로 들어가니 미리 맞춰놓으시면됩니다.

Sub InsertImagesByCell() Dim ws As Worksheet Dim startAddr As String Dim direction As String Dim startCell As Range Dim fd As FileDialog Dim imgPaths As Variant Dim i As Long Dim r As Long, c As Long Dim shp As Shape Set ws = ActiveSheet startAddr = InputBox("이미지를 넣을 시작 셀을 선택하세요") If startAddr = "" Then Exit Sub On Error Resume Next Set startCell = ws.Range(startAddr) On Error GoTo 0 If startCell Is Nothing Then Exit Sub direction = InputBox("방향을 선택하세요. (아래쪽 / 오른쪽)") direction = Trim(direction) If direction <> "아래쪽" And direction <> "오른쪽" Then Exit Sub Set fd = Application.FileDialog(msoFileDialogFilePicker) With fd .AllowMultiSelect = True .Filters.Clear .Filters.Add "Images", "*.jpg; *.jpeg; *.png; *.bmp; *.gif" If .Show <> -1 Then Exit Sub End With ReDim imgPaths(1 To fd.SelectedItems.Count) For i = 1 To fd.SelectedItems.Count imgPaths(i) = fd.SelectedItems(i) Next i r = startCell.Row c = startCell.Column For i = 1 To UBound(imgPaths) Set shp = ws.Shapes.AddPicture( _ Filename:=imgPaths(i), _ LinkToFile:=msoFalse, _ SaveWithDocument:=msoTrue, _ Left:=ws.Cells(r, c).Left, _ Top:=ws.Cells(r, c).Top, _ Width:=ws.Cells(r, c).Width, _ Height:=ws.Cells(r, c).Height) shp.LockAspectRatio = msoFalse shp.Width = ws.Cells(r, c).Width shp.Height = ws.Cells(r, c).Height If direction = "아래쪽" Then r = r + 1 Else c = c + 1 End If Next i End Sub