Translate

ラベル VBA の投稿を表示しています。 すべての投稿を表示
ラベル VBA の投稿を表示しています。 すべての投稿を表示

2016年10月17日月曜日

【VBA】カレンダー作成マクロ

列に並んだ要素を組み合わせて、indesign用のテーブルを作成する仕事があったので、慣れないエクセルを使って作ってみた。
Aの列に日付、B~Fの列に六曜やら九星やらの情報が入っているとする。
それらを組み合わせて、カレンダーを作る。

Sub カレンダー()

Dim hidari As Variant
Dim migi As Variant
Dim currentMonth As Integer
Dim currentRow As Integer
Dim endcell As Integer
Dim readRow As Integer

endcell = Cells(Rows.Count, "A").End(xlUp).Row

hidari = Array("H", "J", "L", "N", "P", "R", "T")
migi = Array("I", "K", "M", "O", "Q", "S", "U")

currentMonth = 1
currentRow = 1 'カレンダーを作る行
Range("H" & currentRow).Value = "日"
Range("J" & currentRow).Value = "月"
Range("L" & currentRow).Value = "火"
Range("N" & currentRow).Value = "水"
Range("P" & currentRow).Value = "木"
Range("R" & currentRow).Value = "金"
Range("T" & currentRow).Value = "土"

currentRow = 2
readRow = 1 '読み取る行

    For readRow = 1 To endcell
    tmpMonth = Month(Range("A" & readRow).Value)
    If currentMonth <> tmpMonth Then
        currentRow = currentRow + 3
        Range("H" & currentRow).Value = "日"
        Range("J" & currentRow).Value = "月"
        Range("L" & currentRow).Value = "火"
        Range("N" & currentRow).Value = "水"
        Range("P" & currentRow).Value = "木"
        Range("R" & currentRow).Value = "金"
        Range("T" & currentRow).Value = "土"
        currentRow = currentRow + 1
        currentMonth = currentMonth + 1
    End If
    Range(hidari((readRow - 1) Mod 7) & currentRow).Value = Day(Range("A" & readRow).Value)
    Range(migi((readRow - 1) Mod 7) & currentRow).Value = Range("B" & readRow).Value & vbLf & Range("C" & readRow).Value & vbLf & Range("D" & readRow).Value & vbLf & Range("E" & readRow).Value
    Range(hidari((readRow - 1) Mod 7) & (currentRow + 1)) = Range("F" & readRow).Value
    If readRow Mod 7 = 0 Then
        currentRow = currentRow + 2
    End If
    Next readRow

End Sub