컴퓨터/엑셀

엑셀 카메라 구역 설정 업데이트 하기

풍경소리^^ 2025. 12. 2. 17:15
Option Explicit

Sub UpdateCamera()
    Dim ws As Worksheet
    Dim cameraShape As Shape
    Dim rangeStart As String, rangeEnd As String, dynamicRange As String
    Dim picRange As Range
    Dim targetCell As Range
    Dim cameraLeft As Double, cameraTop As Double
    
    ' 현재 활성화된 시트 설정
    Set ws = ThisWorkbook.ActiveSheet
    Set targetCell = ws.Range("G18") ' G18 셀
    
    ' 시작 및 끝 범위 가져오기
    rangeStart = ws.Range("AM2").Value '카메라 그림 있는 위치 지정
    rangeEnd = ws.Range("AN2").Value   '카메라 그림 있는 위치 지정
    
    ' 동적 범위 생성
    dynamicRange = rangeStart & ":" & rangeEnd
    Set picRange = ws.Range(dynamicRange)
    
    ' G18에 있는 카메라 그림만 삭제
    For Each cameraShape In ws.Shapes
        If cameraShape.Type = msoPicture Then
            ' 그림의 위치 확인
            cameraLeft = cameraShape.Left
            cameraTop = cameraShape.Top
            
            ' G18 셀과 겹치는 그림만 삭제
            If cameraLeft >= targetCell.Left And cameraLeft < targetCell.Left + targetCell.Width And _
               cameraTop >= targetCell.Top And cameraTop < targetCell.Top + targetCell.Height Then
                cameraShape.Delete
                Exit For ' 해당 그림만 삭제 후 반복 종료
            End If
        End If
    Next cameraShape
    
    ' 새 카메라 그림 생성
    picRange.CopyPicture xlScreen, xlPicture
    ws.Paste
    Set cameraShape = ws.Shapes(ws.Shapes.Count)
    
    ' 그림 위치 설정 (G18 위치에 맞게 수정)
    With cameraShape
        .Left = targetCell.Left
        .Top = targetCell.Top
    End With
End Sub