
연습삼아 공정관리표를 스스로 만들어보고 있습니다.
구현하고자 하는 요소는 다음과 같습니다.
1. 기존에 입력된 월/일/요일 삭제 + 서식 및 셀 병합 초기화
2. 특정 셀에 입력된 연도를 받아와 월/일/요일을 열을 거쳐가며 입력
현재 막힌 지점은 다음과 같습니다.
1. (구현 요소 1.)까진 작동하나, 2.를 구현하기 위해 삽입한 이중for문이 제대로 작동하지 않아 데이터가 입력된 마지막 열의 데이터만 계속 갱신하고 있습니다.
제가 짠 코드 전문과 이해를 돕기위한 사진을 올립니다. 보시고 어떤 부분이 틀렸는지 알려주시면 감사하겠습니다.
Option Explicit
Dim lastRow As Long
Private Sub clearCalendar()
Dim lastColumn As Integer
Dim i As Integer
Application.DisplayAlerts = False
' 셀병합 경고창 비활성화 // application 프로퍼티 조작시 항상 원상복구를 염두에 두고 코딩
For i = 3 To 5 Step 1
lastColumn = Cells(i, Columns.Count).End(xlToLeft).Column
With Range(Cells(i, "O"), Cells(i, lastColumn))
.UnMerge
.ClearContents
End With
Next i
' delete? 아예 지정한 부분을 날려버림(셀 지우기) // clear (셀 유지, 서식+내용 지우기) //
' clearcontents(셀 유지, 내용 지우기) // clearformats(셀 유지, 서식지우기)
Application.DisplayAlerts = True
End Sub
Public Sub automateCalendar()
Dim i As Integer
Dim j As Integer
Dim checkYear As Integer
Dim lastDay As Integer
checkYear = Cells(1, "B").Value
lastDay = Day(DateSerial(checkYear, i + 1, 0))
' lastDay는 dateserial 마지막 일 자리에 0 넣으면 전월 말일 나오는 원리를 사용해 날짜값만 추출
Dim lastDayColumn As Long
Dim lastMonthColumn As Long
' lastDayColumn과 lastMonthColumn은 일자의 마지막에 꼬리물기 하기 위해 선언
Dim dateCheck As String
Application.DisplayAlerts = False
' 셀병합 경고창 비활성화 // application 프로퍼티 조작시 항상 원복을 염두에 두고 코딩
Call clearCalendar
' 입력한 연/월/일 범위 서칭해 삭제
For i = 1 To 12
For j = 1 To lastDay
lastDayColumn = Cells(4, Columns.Count).End(xlToLeft).Column
Cells(4, lastDayColumn + 1).Value = j
' lastdaycolumn 지정해서 월 스택 쌓을 준비 + 일 입력
If j = 1 Then
Cells(4, lastDayColumn + 1).Offset(-1, 0).Value = i
lastMonthColumn = Cells(3, Columns.Count).End(xlToLeft).Column
End If
' 초일이면 i에 맞는 월 입력
If j = lastDay Then
With Range(Cells(3, lastMonthColumn), Cells(4, lastDayColumn + 1).Offset(-1, 0))
.Merge
.Font.Bold = True
.Font.Size = 20
.HorizontalAlignment = xlCenter
End With
End If
' 말일이면 월 셀들 합병하고 서식 조정
dateCheck = Format(DateSerial(checkYear, i, j), "aaa")
' 이중 for 문에 사용할 i, j, 그리고 checkYear까지 합쳐 특정연월일을 만들고, 이를 토대로 특정연월일의 요일을 추출
If dateCheck = "토" Or dateCheck = "일" Then
Cells(4, lastDayColumn + 1).Font.Color = vbRed
With Cells(4, lastDayColumn + 1).Offset(1, 0)
.Value = dateCheck
.Font.Color = vbRed
End With
Else
Cells(4, lastDayColumn + 1).Offset(1, 0).Value = dateCheck
End If
' datecheck를 불러와 토/일요일인 경우 요일, 일 모두 빨간색으로 텍스트 색칠하고 아니면 그냥 요일만 입력
Next j
Next i
Application.DisplayAlerts = True
'셀 병합 경고창 활성화
End Sub