마지막 결의서 내역에서 막혀서요.

☆결의서 내역 자동입력나타내는 방법 ☆

 1. 관목록 시트 = 계정 시트 = 입력 시트 가 같아야 합니다.

 *  관목록 시트 번호와 계정시트 번호는 틀림

2.  B1셀= 관목록시트의 번호이며,  년도 M2셀과 월 N2셀을 조회 할경우

 입력시트의 내용이 결의서내역시트에  일자(일별), 목, 거래처, 적요, 금액, 비고를 나타내주고 마지막에 목별 소계나타내기(배경색깔 넣기), 총합계나타내기(배경색깔 및 목소계별 합계)를 하고 싶습니다.

   *VBA가 맞는지 확인 부탁드리며 자동서식이 될수 있게 도와주세요.

감사합니다.

Sub 결의서_완성_자동출력()

Dim wsIn As Worksheet, wsOut As Worksheet, wsAcc As Worksheet
Dim lastRow As Long, outRow As Long
Dim i As Long
Dim 기준번호, 기준관 As String
Dim startDate As Date, endDate As Date

Set wsIn = Sheets("입력")
Set wsOut = Sheets("결의서 내역")
Set wsAcc = Sheets("계정시트")

' 1 번호 → 관 자동
기준번호 = wsOut.Range("A1").Value

Dim f As Range
Set f = wsAcc.Range("A:A").Find(기준번호, lookat:=xlWhole)

If f Is Nothing Then
MsgBox "번호 오류!", vbExclamation
Exit Sub
End If

기준관 = wsAcc.Cells(f.Row, 2).Value
wsOut.Range("B1").Value = 기준관

' 2 연/월
startDate = DateSerial(wsOut.Range("L2").Value, wsOut.Range("M2").Value, 1)
endDate = WorksheetFunction.EoMonth(startDate, 0)

' 제목 자동
wsOut.Range("A2").Value = wsOut.Range("L2").Value & "년 " & wsOut.Range("M2").Value & "월 결의서"

' 초기화
wsOut.Range("A3:G1000").Clear
wsOut.Range("A3:G1000").Interior.ColorIndex = xlNone

lastRow = wsIn.Cells(wsIn.Rows.Count, "A").End(xlUp).Row
outRow = 3

' 3 데이터 입력
For i = 4 To lastRow

If wsIn.Cells(i, 3).Value = 기준관 Then

If wsIn.Cells(i, 2).Value >= startDate And wsIn.Cells(i, 2).Value <= endDate Then

wsOut.Cells(outRow, 1).Value = wsIn.Cells(i, 1).Value
wsOut.Cells(outRow, 2).Value = wsIn.Cells(i, 2).Value
wsOut.Cells(outRow, 3).Value = wsIn.Cells(i, 5).Value
wsOut.Cells(outRow, 4).Value = wsIn.Cells(i, 6).Value
wsOut.Cells(outRow, 5).Value = wsIn.Cells(i, 7).Value
wsOut.Cells(outRow, 6).Value = wsIn.Cells(i, 8).Value
wsOut.Cells(outRow, 7).Value = wsIn.Cells(i, 9).Value

outRow = outRow + 1

End If

End If

Next i

If outRow = 3 Then
MsgBox "데이터 없음", vbInformation
Exit Sub
End If

' 4 정렬
With wsOut.Sort
.SortFields.Clear
.SortFields.Add Key:=wsOut.Range("A3:A" & outRow - 1), Order:=xlAscending
.SortFields.Add Key:=wsOut.Range("B3:B" & outRow - 1), Order:=xlAscending
.SetRange wsOut.Range("A3:G" & outRow - 1)
.Header = xlNo
.Apply
End With

' 5 목별 소계
Dim current목 As String, subtotal As Double
current목 = wsOut.Cells(3, 3).Value
subtotal = 0

For i = 3 To outRow - 1

If wsOut.Cells(i, 3).Value <> current목 Then

wsOut.Rows(i).Insert
wsOut.Cells(i, 5).Value = "소계"
wsOut.Cells(i, 6).Value = subtotal
wsOut.Range("A" & i & ":G" & i).Interior.Color = RGB(220, 230, 241)

subtotal = 0
current목 = wsOut.Cells(i + 1, 3).Value
outRow = outRow + 1
i = i + 1

End If

subtotal = subtotal + wsOut.Cells(i, 6).Value

Next i

' 마지막 소계
wsOut.Rows(outRow).Insert
wsOut.Cells(outRow, 5).Value = "소계"
wsOut.Cells(outRow, 6).Value = subtotal
wsOut.Range("A" & outRow & ":G" & outRow).Interior.Color = RGB(220, 230, 241)

' 6 총합
Dim total As Double
total = Application.WorksheetFunction.Sum(wsOut.Range("F3:F" & outRow))

wsOut.Rows(outRow + 1).Insert
wsOut.Cells(outRow + 1, 5).Value = "총합계"
wsOut.Cells(outRow + 1, 6).Value = total
wsOut.Range("A" & outRow + 1 & ":G" & outRow + 1).Interior.Color = RGB(180, 198, 231)

' 7 서식 (보기 좋게)
wsOut.Range("A3:G" & outRow + 1).Borders.LineStyle = xlContinuous
wsOut.Columns("A:G").AutoFit

MsgBox "결의서 완성 완료", vbInformation

End Sub