You are on page 1of 2

Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("2024 년")

' 기존 변수 재정의 및 초기화


Dim rng As Range, cell As Range
Dim lastRow As Long, count As Long
Dim sumValue As Double, sumValueReport As Double
Dim white As Long, monthValue As Integer
Dim result As Integer, startCol As Integer

white = RGB(255, 255, 255)


count = 1
sumValue = 0
sumValueReport = 0
result = 0

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

' 코드 1 통합 (문서 2+3+4.txt 에서)


If Not Intersect(Target, Me.Columns("A")) Is Nothing Or Not Intersect(Target,
Me.Columns("E")) Is Nothing Or Not Intersect(Target, Me.Columns("F")) Is Nothing
Then
' A 열에 대한 처리
lastRow = Cells(Rows.count, "A").End(xlUp).Row
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

' E 열에 대한 처리 (문서 2+3+4.txt 에서)


If Not Intersect(Target, Me.Columns("E")) Is Nothing Then
Set rng = Range("E11:E" & Me.Range("연구마지막").Row - 1)
For Each cell In rng
If 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

' F 열에 대한 처리 (문서 2+3+4.txt 에서)


If Not Intersect(Target, Me.Columns("F")) Is Nothing Then
Set rng = Range("F11:F" & Me.Range("보고마지막").Row - 1)
For Each cell In rng
If 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
End If

' 코드 2 통합 (문서 7+8.txt 에서)


monthValue = ws.Range("월값").Value
Dim checkRange As Range
Set checkRange = ws.Range("B11:B" & ws.Range("B 보고자").Row - 1)
For Each cell In checkRange
If cell.Interior.Color <> RGB(221, 235, 247) Then
' 병합 셀 확인을 위한 추가 로직
If cell.MergeCells Then
If cell.Address = cell.MergeArea.Cells(1, 1).Address Then
count = count + 1
End If
End If

' "월값"에 따른 시작 열 설정
startCol = 12 + monthValue - 1
While ws.Cells(cell.Row, startCol).Interior.Color = RGB(208, 206, 206)
startCol = startCol - 1
Wend

' 결과값 증가 조건 확인
If ws.Cells(cell.Row, startCol).Interior.Color = RGB(0, 176, 80) Then
result = result + 1
End If
End If
Next cell

' 결과 저장
ws.Range("결과값").Value = result
ws.Range("담당업체").Value = count - 1

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


End Sub

You might also like