ちょっと生きづらさを抱えた人へ、気持ちを楽にするためのお手紙です。

【VBA】次の月曜日・火曜日・水曜日・木曜日・金曜日・土曜日・日曜日を求める

筆箱にカンニングペーパーを入れる係のみすくです。こんにちは。
VBA関連だけまとめたブログもあります。
筆箱VBA
Public Sub main()
    Debug.Print GetNextSaturday(#1/27/2019#)
    Debug.Print GetNextSaturday(#1/28/2019#)
    Debug.Print GetNextSaturday(#1/29/2019#)
    Debug.Print GetNextSaturday(#1/30/2019#)
    Debug.Print GetNextSaturday(#1/31/2019#)
    Debug.Print GetNextSaturday(#2/1/2019#)
    Debug.Print GetNextSaturday(#2/2/2019#)
End Sub

'---------------------------------------------------------------------------------------------------
'【処 理 名】次の月曜日の日付取得
'【処理概要】指定の日付の次の月曜日の日付を取得する
'【引    数】[I]ByVal dt As Date    基準日
'【返 却 値】次の月曜日の日付
'---------------------------------------------------------------------------------------------------
Private Function GetNextMonday(ByVal dt As Date) As Date
    GetNextMonday = dt + (7 - Weekday(dt, vbMonday)) + 1
End Function

'---------------------------------------------------------------------------------------------------
'【処 理 名】次の火曜日の日付取得
'【処理概要】指定の日付の次の火曜日の日付を取得する
'【引    数】[I]ByVal dt As Date    基準日
'【返 却 値】次の火曜日の日付
'---------------------------------------------------------------------------------------------------
Private Function GetNextTuesday(ByVal dt As Date) As Date
    GetNextTuesday = dt + (7 - Weekday(dt, vbTuesday)) + 1
End Function

'---------------------------------------------------------------------------------------------------
'【処 理 名】次の水曜日の日付取得
'【処理概要】指定の日付の次の水曜日の日付を取得する
'【引    数】[I]ByVal dt As Date    基準日
'【返 却 値】次の水曜日の日付
'---------------------------------------------------------------------------------------------------
Private Function GetNextWednesday(ByVal dt As Date) As Date
    GetNextWednesday = dt + (7 - Weekday(dt, vbWednesday)) + 1
End Function

'---------------------------------------------------------------------------------------------------
'【処 理 名】次の木曜日の日付取得
'【処理概要】指定の日付の次の木曜日の日付を取得する
'【引    数】[I]ByVal dt As Date    基準日
'【返 却 値】次の木曜日の日付
'---------------------------------------------------------------------------------------------------
Private Function GetNextThursday(ByVal dt As Date) As Date
    GetNextThursday = dt + (7 - Weekday(dt, vbThursday)) + 1
End Function

'---------------------------------------------------------------------------------------------------
'【処 理 名】次の金曜日の日付取得
'【処理概要】指定の日付の次の金曜日の日付を取得する
'【引    数】[I]ByVal dt As Date    基準日
'【返 却 値】次の金曜日の日付
'---------------------------------------------------------------------------------------------------
Private Function GetNextFriday(ByVal dt As Date) As Date
    GetNextFriday = dt + (7 - Weekday(dt, vbFriday)) + 1
End Function

'---------------------------------------------------------------------------------------------------
'【処 理 名】次の土曜日の日付取得
'【処理概要】指定の日付の次の土曜日の日付を取得する
'【引    数】[I]ByVal dt As Date    基準日
'【返 却 値】次の土曜日の日付
'---------------------------------------------------------------------------------------------------
Private Function GetNextSaturday(ByVal dt As Date) As Date
    GetNextSaturday = dt + (7 - Weekday(dt, vbSaturday)) + 1
End Function

'---------------------------------------------------------------------------------------------------
'【処 理 名】次の日曜日の日付取得
'【処理概要】指定の日付の次の日曜日の日付を取得する
'【引    数】[I]ByVal dt As Date    基準日
'【返 却 値】次の日曜日の日付
'---------------------------------------------------------------------------------------------------
Private Function GetNextSunday(ByVal dt As Date) As Date
    GetNextSunday = dt + (7 - Weekday(dt, vbSunday)) + 1
End Function
Weekday
何曜日であるかを表す 1 (日曜) ~ 7 (土曜) の範囲の値を返します。
Weekday(date, [firstdayofweek])
 
引数
date
 日付を表す式を指定します。
 引数 date が有効な値ではない場合は、Null 値を返します。
firstdayofweek
 週の第 1 日目の曜日を表す定数を指定します。
 この値を省略すると、1 (日曜) が使用されます。

Weekday関数の第2引数を指定し、
求めたい曜日が週の始まりになるようにずらして、
基準日とその週までの差分を求めて計算しています。

例えば、第2引数にvbMondayを指定すると、
第1引数の日付が月曜日の場合1、日曜日は7が返却されます。
7日間(1週間)からWeekdayの返却値を引けば、
週末までの日数がわかります。

基準日にその日数を足して、その次の日が求めたい曜日の日。


参考
Weekday 関数MSDN

コメントを残す

メールアドレスが公開されることはありません。

CAPTCHA