XLS+VBA
365 엑셀에서 셀안의 그림(PictureInCell) 기능
쵸코난
2024. 12. 23. 04:10
365 엑셀의 새기능인 셀안의 그림(PictureInCell) 기능입니다.
마치 아래 한/글에서 그림을 글자 취급하는 것처럼
엑셀 시트에서 그림을 셀안에 종속적으로 삽입할 수 있습니다.
글자처럼 간주되어 정렬도 가능합니다.
우클릭해서 '셀 위에 놓기'를 선택해서 일반 그림으로 변경할 수도 있습니다.
반대로 일반 그림을 '셀에 배치'를 선택해서 '셀안의그림'으로 바꿀 수도 있습니다.
셀에서 사용자 함수인 Image 를 이용할 수 있습니다.
=IMAGE(source, [alt_text], [sizing], [height], [width])
세번째 인수:
0 이미지를 셀에 맞추고 가로 세로 비율을 유지합니다.
1 셀을 이미지로 채우고 가로 세로 비율을 무시합니다.
2 셀 경계를 초과할 수 있는 원본 이미지 크기를 유지합니다.
3 높이 및 너비 인수를 사용하여 이미지 크기를 사용자 지정합니다.
예시:
=IMAGE("https://support.content.office.net/en-us/media/2d9e717a-0077-438f-8e5e-f85a1305d4ad.jpg", "Sphere")
=IMAGE(A2, B2, 0)
=IMAGE(A3, B3, 1)
참고: 마이크로소프트 도움말
이제 이것을 VBA로 처리하는 방법들입니다.
'그림파일을 셀안에 그림으로 삽입
Sub RangePictureInCell()
Dim rng As Range
Set rng = ActiveCell
rng.InsertPictureInCell "C:\그림파일경로\그림.png"
End Sub
'현재 선택된 그림을 셀안의 그림으로 변환
Sub PlaceSelectedPictureInCell()
Dim shp As Shape
Set shp = ActiveWindow.Selection.ShapeRange(1)
shp.PlacePictureInCell
End Sub
'현재 셀의 그림을 셀 위에 그림으로 변환
Sub PlacePictureOverCell()
Dim rng As Range
Set rng = ActiveCell
rng.PlacePictureOverCells
End Sub
고전적으로 그림을 셀에 삽입하는 방법은 두 가지가 있습니다.
'https://stackoverflow.com/questions/12936646/how-to-insert-a-picture-into-excel-at-a-specified-cell-position-with-vba
Sub InsertPicture()
Dim destRange As Range
Dim ImageName As String
Set destRange = ActiveCell
ImageName = "C:\그림파일경로\그림.png"
On Error Resume Next
'
' first and faster method (in Office 2016)
'
With ws.Pictures.Insert(Filename:=imageFileName, LinkToFile:=msoTrue, SaveWithDocument:=msoTrue)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = destRange.Width
.Height = destRange.Height
End With
.Left = destRange.Left
.Top = destRange.Top
.Placement = 1
.PrintObject = True
.Name = ImageName
End With
'
' second but slower method (in Office 2016)
'
If Err.Number <> 0 Then
Err.Clear
Dim myPic As Shape
Set myPic = ws.Shapes.AddPicture(Filename:=imageFileName, _
LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
Left:=destRange.Left, Top:=destRange.Top, Width:=-1, Height:=destRange.Height)
With myPic.OLEFormat.Object.ShapeRange
.LockAspectRatio = msoTrue
.Width = destRange.Width
.Height = destRange.Height '222
End With
End If
End Sub
샘플 파일입니다.