You are on page 1of 2

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 count As Long
Dim white As Long
white = RGB(255, 255, 255)

Application.EnableEvents = False ' 이벤트 중단


On Error Resume Next

' "보고마지막" 탐색
lastRow = Me.Range("보고마지막").Row - 1
' F 열 변동 시 "보고값" 계산
If Not Intersect(Target, Me.Columns("F")) Is Nothing Then
sumValue = 0 ' sumValue 초기화
Set rng = Me.Range("F11:F" & lastRow) ' F 열 변동 감지
For Each cell In rng
If (cell.MergeCells And cell.Interior.Color = white) Or _
(Not cell.MergeCells And cell.Interior.Color = white) Then
sumValue = sumValue + CDbl(cell.Value)
End If
Next cell
Me.Range("보고값").Value = sumValue ' 결과값 "보고값" 입력
End If

' "연구마지막" 탐색
lastRow = Me.Range("연구마지막").Row - 1
' E 열 변동 시 "인원값" 계산
If Not Intersect(Target, Me.Columns("E")) Is Nothing Then
sumValue = 0 ' sumValue 초기화
Set rng = Me.Range("E11:E" & lastRow) ' E 열 변동 감지
For Each cell In rng
If (cell.MergeCells And cell.Interior.Color = white) Or _
(Not cell.MergeCells And cell.Interior.Color = white) Then
sumValue = sumValue + CDbl(cell.Value)
End If
Next cell
Me.Range("인원값").Value = sumValue ' 결과값 "인원값" 입력
End If

' A 열 변동 시 숫자 카운트 및 "보고자" 관련 처리


If Not Intersect(Target, Me.Columns("A")) Is Nothing Then
count = 1 ' 카운트 초기화
Set rng = Me.Range("A11:A" & lastRow) ' A11 부터 lastRow 까지 범위 설정

For Each cell In rng.Cells


' 병합되어 있고, 병합된 셀 중 첫 번째 셀일 경우에만 카운트를 증가
If cell.MergeArea.Cells(1, 1).Address = cell.Address Then
cell.Value = count
count = count + 1

' 병합된 셀이 있는 경우, 병합된 만큼 셀 카운트를 건너뜀


If cell.MergeArea.count > 1 Then
Set cell = Me.Cells(cell.MergeArea.Row +
cell.MergeArea.Rows.count - 1, "A")
End If
End If
Next cell
End If
' 이벤트 다시 활성화
Application.EnableEvents = True
' 에러 핸들링 정상화
On Error GoTo 0
End Sub

You might also like