You are on page 1of 1

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range


Dim cell As Range
Dim lastRow As Long
Dim sumValue As Double
Dim white As Long
white = RGB(255, 255, 255)

' 마지막 행 찾기
lastRow = Me.Range("연구마지막").Row - 1

' 변동이 있는 경우
If Not Intersect(Target, Me.Columns("E")) Is Nothing Then
Application.EnableEvents = False ' 이벤트 중단하여 무한 루프 방지

' "연구마지막"이라는 문자열이 있는 곳의 바로 전 열까지를 범위로 설정


Set rng = Range("E11:E" & lastRow)

' 범위에 대해 처리
For Each cell In rng
' 병합된 셀이면서 배경색이 흰색인 경우
If cell.MergeCells And cell.Interior.Color = white Then
' 첫 번째 값만 합계에 더함 (문자열이거나 숫자가 아닌 경우 오류가 발생할 수 있으므로 예외
처리)
On Error Resume Next
sumValue = sumValue + CDbl(cell.Value)
On Error GoTo 0
' 병합되지 않은 셀이면서 배경색이 흰색인 경우
ElseIf Not cell.MergeCells And cell.Interior.Color = white Then
' 셀 값을 합계에 더함 (문자열이거나 숫자가 아닌 경우 오류가 발생할 수 있으므로 예외 처리)
On Error Resume Next
sumValue = sumValue + CDbl(cell.Value)
On Error GoTo 0
End If
Next cell

' "인원값"셀에 합계 값을 입력
Me.Range("인원값").Value = sumValue

Application.EnableEvents = True ' 이벤트 다시 활성화


End If
End Sub

You might also like