Option Explicit

Private Sub UserForm_Initialize()
    Dim m As Long
    cbo월.Clear
    cbo월.Style = fmStyleDropDownList
    For m = 1 To 12
        cbo월.AddItem Format$(m, "00")
    Next m
    txt폴더.Value = ThisWorkbook.Path
    txt금액.Value = "120000"
    txt기준일.Value = "2026-09-01"
    chk동의.Value = False
End Sub

Private Function FolderExists(ByVal path As String) As Boolean
    On Error GoTo NotFound
    FolderExists = ((GetAttr(path) And vbDirectory) = vbDirectory)
    Exit Function
NotFound:
    FolderExists = False
End Function

Private Function ValidISODate(ByVal value As String) As Boolean
    Dim y As Long, m As Long, d As Long, parsed As Date
    If Not value Like "####-##-##" Then Exit Function
    On Error GoTo InvalidDate
    y = CLng(Left$(value, 4))
    m = CLng(Mid$(value, 6, 2))
    d = CLng(Right$(value, 2))
    If y < 1900 Or y > 2099 Or m < 1 Or m > 12 Or d < 1 Or d > 31 Then Exit Function
    parsed = DateSerial(y, m, d)
    ValidISODate = (Year(parsed) = y And Month(parsed) = m And Day(parsed) = d)
    Exit Function
InvalidDate:
    ValidISODate = False
End Function

Private Function 입력검증() As Boolean
    Dim amount As String, path As String
    path = Trim$(CStr(txt폴더.Value))
    If Len(path) = 0 Then
        MsgBox "폴더 경로를 입력하세요.": txt폴더.SetFocus: Exit Function
    End If
    If Not FolderExists(path) Then
        MsgBox "읽을 수 있는 폴더를 선택하세요.": txt폴더.SetFocus: Exit Function
    End If
    amount = Trim$(CStr(txt금액.Value))
    If Len(amount) = 0 Then
        MsgBox "금액을 입력하세요.": txt금액.SetFocus: Exit Function
    End If
    If amount Like "*[!0-9]*" Or Len(amount) > 9 Then
        MsgBox "금액은 쉼표 없는 0~999999999 정수로 입력하세요.": txt금액.SetFocus: Exit Function
    End If
    If Not ValidISODate(CStr(txt기준일.Value)) Then
        MsgBox "1900~2099 범위의 실제 날짜를 YYYY-MM-DD로 입력하세요.": txt기준일.SetFocus: Exit Function
    End If
    If cbo월.ListIndex < 0 Then
        MsgBox "월을 목록에서 선택하세요.": cbo월.SetFocus: Exit Function
    End If
    If chk동의.Value <> True Then
        MsgBox "연습 입력 확인에 체크하세요.": chk동의.SetFocus: Exit Function
    End If
    입력검증 = True
End Function

Private Sub btn실행_Click()
    If Not 입력검증() Then Exit Sub
    MsgBox "검증 통과: 금액=" & CStr(CLng(Trim$(CStr(txt금액.Value)))) _
        & ", 기준일=" & CStr(txt기준일.Value) & ", 월=" & CStr(cbo월.Value) _
        & vbCrLf & "연습 모드: 파일을 생성하거나 저장하지 않았습니다.", vbInformation
End Sub
