LoginSignup
0
2

More than 3 years have passed since last update.

OpenOffice CalcでExcel VBAのマクロを組む(2)

Posted at

実行イメージ

まずは、月単位での日付と曜日をセルに出力するマクロを組んでみた。
E1セルとE2セルに月単位での期間を入力(例えば2019年7月ならE1セルに「2019/07/01」、E2セルに「2019/07/31」と入力)し、F1セルとF2セルにそれぞれ対応する曜日を入力してマクロを実行すると、
2019092801.JPG
A3セル以降に日付、B3セル以降に曜日を出力するという内容。
2019092802.JPG

サンプルマクロ

今回組んだマクロは以下の通り。

option vbasupport 1

Sub Main

    REM E列2行目に指定した月末日から日部分だけ取り出す
    Dim lastDay As String
    lastDay = Cells(2, 5)
    lastDay = Right(lastDay, 2)

    Dim dayNum As Integer
    dayNum = CInt(lastDay)

    REM 曜日フラグの設定
    Dim dayFlag  As Integer

    If (StrComp(Cells(1, 6), "月", vbTextCompare ) = 0) Then
        REM 月初日が月曜日
        dayFlag = 1
    ElseIf (StrComp(Cells(1, 6), "火", vbTextCompare ) = 0) Then
        REM 月初日が火曜日
        dayFlag = 2
    ElseIf (StrComp(Cells(1, 6), "水", vbTextCompare ) = 0) Then
        REM 月初日が水曜日
         dayFlag = 3
    ElseIf (StrComp(Cells(1, 6), "木", vbTextCompare ) = 0) Then
        REM 月初日が木曜日
        dayFlag = 4
    ElseIf (StrComp(Cells(1, 6), "金", vbTextCompare ) = 0) Then
        REM 月初日が金曜日
        dayFlag = 5
    ElseIf (StrComp(Cells(1, 6), "土", vbTextCompare ) = 0) Then
        REM 月初日が土曜日
        dayFlag = 6
    Else
        REM 月初日が日曜日
        dayFlag = 7
    End If

    Dim dayCnt As Integer
    For dayCnt = 1 To 31
        If (dayCnt <= dayNum) Then
            REM 月末日までの日をA列の3行目から順に記入する
            Cells(dayCnt + 2, 1) = dayCnt

            REM 曜日を記入する
            Select Case dayFlag
            Case 1
                Cells(dayCnt + 2, 2) = "月"
            Case 2
                Cells(dayCnt + 2, 2) = "火"
            Case 3
                Cells(dayCnt + 2, 2) = "水"
            Case 4
                Cells(dayCnt + 2, 2) = "木"
            Case 5
                Cells(dayCnt + 2, 2) = "金"
            Case 6
                Cells(dayCnt + 2, 2) = "土"
            Case Else
                Cells(dayCnt + 2, 2) = "日"
            End Select

            dayFlag = dayFlag + 1
            If (dayFlag > 7) Then
                REM 日曜まで到達したら、曜日フラグをリセット
                dayFlag = 1
            End If
        Else
            REM 月末日が31日より前の場合は、月末日の次の日から31日までの範囲を曜日も含めて空欄表示にする
            Cells(dayCnt + 2, 1) = ""
            Cells(dayCnt + 2, 2) = ""
        End If
    Next dayCnt

End Sub

なお、VBA互換モードでマクロを組むのであれば、先頭に「option vbasupport 1」の記載が必要になる。

[参考にさせていただいたページ]
OpenOffice Calc のOpenOfficeBasicをVBA互換モードにする方法
https://toolmania.info/post-2794/
CalcでExcelのVBAマクロを含む表計算シートを使う
http://www.programmingmat.jp/openoffice_macro/ooo_bmvba.html
OpenOffice日本語版
https://www.openoffice.org/ja/

0
2
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
2