0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Excel VBA 奮闘記 カレンダー(Write Only Code)

Posted at

ほとんど書捨て(Write Only)に近いけれどカレンダーらしきものを書いた。
かなり完成度は低い(-_-;)

実行結果
cal.PNG

Option Explicit

Dim dt As Date
Dim last As Integer
Const start_day As Integer = 1
Const limit As Integer = 8

Private Static Sub main()
    
    Dim tmp As Range
    Set tmp = Worksheets(Me.name).Range("A1")
    Dim remain As Integer

    remain = 0

    Call init
    Call this_month_end

    remain = print_week(tmp, 1, first_weekday(), limit - first_weekday(), 0)
    remain = print_week(tmp, 3, vbSunday, vbSaturday, remain)
    remain = print_week(tmp, 5, vbSunday, vbSaturday, remain)
    remain = print_week(tmp, 7, vbSunday, vbSaturday, remain)
    remain = print_week(tmp, 9, vbSunday, vbSaturday, remain)
   
End Sub

Private Sub init()
    dt = 0
    last = 0

End Sub

Private Sub this_month_end()
    Const pre As Double = -1
    ' 次月の末日
    dt = DateSerial(Year(Date), Month(Date) + 1, start_day)
    ' 今月の月末の日付
    dt = DateAdd("d", pre, dt)
    ' 日のみ抽出
    last = day(dt)
End Sub

Function first_weekday() As Integer
    Dim wk As Date

    ' 月初の曜日取得
    wk = DateSerial(Year(Date), Month(Date), start_day)

    ' 日のみ抽出して返す
    first_weekday = Weekday(wk)
    
End Function

' 第n週を表示
Function print_week(ByVal place As Range, ByVal row_step As Integer, ByRef start_pos As Integer, ByRef end_pos As Integer, ByVal day_val As Integer) As Integer
    Dim i As Integer
    Dim day_remain As Integer

    day_remain = day_val

    i = 0
    Do While i < end_pos
        place(row_step, start_pos + i).Value = incr(day_remain)
        i = i + 1
    Loop

     print_week = day_remain

End Function

' 値のインクリメント
Function incr(ByRef dd As Integer)
    dd = dd + 1
    
    If dd > last Then
        Exit Function
    End If

    incr = dd
End Function
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?