Picture

셀의 값에 해당하는 사진 파일을 불러와 삽입

노만e 2025. 3. 26. 21:38

🛠 사용 방법

  1. 엑셀 시트에서 특정 셀에 이미지 파일명(예: "employee01", "productA" 등)을 입력.
  2. 매크로를 실행하면 해당 파일명을 기반으로 이미지가 삽입
  3. 이미지가 있는 경우 기존 이미지는 삭제되고, 새로운 이미지가 설정된 위치(3x3 셀 크기)에 삽입됨.
  4. 선택한 여러 개의 셀을 한 번에 실행할 수도 있음.
  5. 단축키 ctrl + w를 눌러서 실행할 수도 있음.

🛠 단축키 만드는  방법

1. alt + F8을 누르면 아래와 같이 [매크로] 창이 나옴.

2. 실행하려는 매크로 이름을 선택후, [옵션]버튼 클릭

3. [매크로 옵션]창이 나오는데, 바로가기키를 등록후, [확인]버튼 누르면 등록완료 

매크로 단축키를 만드는 방법

 

 

Sub InsertImageintheCellValue()
    ' 선택된 셀에서 값을 읽어 해당하는 이미지를 삽입하는 매크로
    
    Dim cell As Range
    Dim imageName As String
    Dim shape As shape
    Dim imgLeft As Single, imgTop As Single, imgWidth As Single, imgHeight As Single
    Dim targetRange As Range
    Dim imgPath As String
    
    For Each cell In Selection.Cells
        If Not IsEmpty(cell) Then
            
            Dim row As Integer, col As Integer
            row = cell.row
            col = cell.Column
            
            imageName = Cells(row, col).Value
            
            If imageName <> vbNullString Then
                
                ' 이미지 경로 설정
                imgPath = ThisWorkbook.Path & "\Image\" & imageName & ".jpg"
                
                ' 이미지가 존재하는 경우에만 실행
                If Dir(imgPath) <> vbNullString Then
                    
                    ' 이미지 삽입 위치 설정 (현재 셀 기준 3행 위에서 3x3 영역)
                    Set targetRange = Range(Cells(row - 3, col), Cells(row - 1, col + 2))
                    imgLeft = targetRange.Left + 1
                    imgWidth = targetRange.Width - 2
                    imgTop = targetRange.Top + 1
                    imgHeight = targetRange.Height - 2
                    
                    ' 기존 이미지 삭제
                    For Each shape In ActiveSheet.Shapes
                        If Not Intersect(targetRange, shape.TopLeftCell) Is Nothing Then
                            shape.Delete
                        End If
                    Next shape
                    
                    ' 새 이미지 삽입
                    Set shape = ActiveSheet.Shapes.AddPicture(imgPath, msoFalse, msoCTrue, imgLeft, imgTop, imgWidth, imgHeight)
                    
                    ' 이미지 테두리 설정
                    With shape.Line
                        .Visible = msoTrue
                        .ForeColor.ObjectThemeColor = msoThemeColorText1
                        .Weight = 0.25
                    End With
                    
                End If
            End If
        End If
    Next cell
End Sub

 

 

InsertImageintheCellValue.zip
1.01MB