Download as txt, pdf, or txt
Download as txt, pdf, or txt
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 count As Long
Dim sumValue As Double
Dim white As Long
Dim sumValueReport As Double
white = RGB(255, 255, 255)

' 변수 초기화
count = 1
sumValue = 0
sumValueReport = 0

Application.EnableEvents = False ' 이벤트 중단하여 무한 루프 방지

' 코드 1
lastRow = Cells(Rows.count, "A").End(xlUp).Row
If Not Intersect(Target, Me.Columns("A")) Is Nothing Then
For i = 11 To lastRow
Set cell = Cells(i, "A")
If cell.Value <> "보고자" And cell.Column = 1 Then
cell.Value = count
If cell.MergeArea.count > 1 Then
i = i + cell.MergeArea.count - 1
End If
count = count + 1
ElseIf cell.Value = "보고자" Then
If Cells(i - 1, "A").MergeArea.count > 1 Then
Range("A8").Value = Cells(i - 1, "A").MergeArea(1, 1).Value
Else
Range("A8").Value = Cells(i - 1, "A").Value
End If
End If
Next i
End If

' 코드 2
lastRow = Me.Range("연구마지막").Row - 1
If Not Intersect(Target, Me.Columns("E")) Is Nothing Then
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
End If

' 코드 3
lastRow = Me.Range("보고마지막").Row - 1
If Not Intersect(Target, Me.Columns("F")) Is Nothing Then
Set rng = Range("F11:F" & lastRow)
For Each cell In rng
If cell.MergeCells And cell.Interior.Color = white Then
On Error Resume Next
sumValueReport = sumValueReport + CDbl(cell.Value)
On Error GoTo 0
ElseIf Not cell.MergeCells And cell.Interior.Color = white Then
On Error Resume Next
sumValueReport = sumValueReport + CDbl(cell.Value)
On Error GoTo 0
End If
Next cell
Me.Range("보고값").Value = sumValueReport
End If

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


End Sub

You might also like