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 1 year has passed since last update.

スケジュール管理ExcelVBA

Last updated at Posted at 2024-04-04

他環境で試していないソースを記述するのはどうかと思いつつ
スケジュール管理が手いっぱいになってきたので管理用の通知を作成してみた。

簡単な仕様としては
スケジュールシートを作成し
A列:日付
B列:時間
C列:メッセージ
D列:チェック(OKが入力される)

image.png

仕様としては

10分後のタイミングで本日日付とD列とE列が合致及びF列にOKが無い状態のレコードを通知バックグラウンドで立ち上げた状態で時間が来たらC列を通知する

■リマインド部分

Public Sub ScheduleReminder()
    Dim ws As Worksheet
    Dim targetDate As Date
    Dim targetTime As Date
    Dim message As String
    Dim lastRow As Long
    Dim i As Long
    Dim reminderFlag As Boolean

    ' スケジュールが記録されたシートを指定します
    Set ws = ThisWorkbook.Sheets("スケジュール")

    ' 今日の日付と時間を取得します
    targetDate = Date
    targetTime = Time

    ' 最終行を取得します
    lastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row

    ' スケジュールをチェックし、条件に合致する場合に通知を行います
    For i = 2 To lastRow ' 1行目はヘッダーなので無視します
        Dim scheduledDateTime As Date
        Dim scheduledtargetDateTime As Date
        scheduledDateTime = DateValue(Format(ws.Cells(i, "D").Value, "YYYY/MM/DD")) + TimeValue(Format(ws.Cells(i, "E").Value, "hh:mm"))
        scheduledtargetDateTime = targetDate + targetTime
        If scheduledDateTime <= scheduledtargetDateTime And ws.Cells(i, "F").Value <> "OK" Then
            ' スケジュールされた日時が現在の日時を過ぎている場合に通知を行います
            message = ws.Cells(i, "B").Value
            MsgBox "リマインダーメッセージ: " & message, vbInformation
            ' リマインダーが存在することを示すフラグを立てる
            reminderFlag = True
        End If
    Next i

    ' リマインダーフラグがTrueでない場合、リマインダーは存在しない
    If reminderFlag = False Then
        MsgBox "リマインダーメッセージはありません。", vbInformation
    End If
    ' タイマー再設定
    StartTimer
End Sub

■タイマー設定(10分後に再度実行)

Sub StartTimer()
    ' 10分ごとにマクロを実行するためのタイマーを開始します
    Application.OnTime Now + TimeValue("00:10:00"), "ScheduleReminder"
End Sub

■タイマー停止

Sub StopTimer()
    ' タイマーを停止します
    On Error Resume Next
    Application.OnTime EarliestTime:=Now + TimeValue("00:10:00"), Procedure:="ScheduleReminder", _
        Schedule:=False
    On Error GoTo 0
End Sub

本来は立ち上げたタイミングで実行したかったが手動実行ボタンなどを付けて一度実行させておけばあとはタイマーが起動してくれるはず。

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?