Sub 日付計算8日前()
’C列の日付をYYYYMMDD形式からDateSerial関数を使って日付型に変換
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim dateStr As String
Dim dateValue As Date
Dim workdayDate As Date
' 対象のワークシートを設定
Set ws = ThisWorkbook.Sheets("Sheet2")
' 最終行を取得
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
' C列の値を変換してG列に書き込む
For i = 1 To lastRow
dateStr = ws.Cells(i, "C").Value
' 日付の形式が正しいか確認
If Len(dateStr) = 8 And IsNumeric(dateStr) Then
' 年、月、日の部分を抽出して日付として認識
dateValue = DateSerial(Left(dateStr, 4), Mid(dateStr, 5, 2), Right(dateStr, 2))
' WORKDAY関数を使って-8日前の日付を計算
workdayDate = Application.WorksheetFunction.WorkDay(dateValue, -8)
' G列にyyyy/mm/dd形式で書き込み
ws.Cells(i, "G").Value = Format(workdayDate, "yyyy/mm/dd")
Else
' 日付の形式が正しくない場合、G列を空白にする
ws.Cells(i, "G").Value = ""
End If
Next i
End Sub