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?

【Excel VBA】FileDateTime関数|ファイルの最終更新日時を取得する方法と注意点

Last updated at Posted at 2025-08-18

この記事ではFileDateTime関数の使い方と注意点について解説します。
他のよく使うVBA関数一覧はこちら。

FileDateTime関数の使い方と注意点

FileDateTime関数は、指定したファイルの「最終更新日時」を取得する関数です。
ファイル管理や変更監視などで便利に使えます。

構文

FileDateTime(パス)
  • パス : 対象ファイルまたはフォルダのフルパス(文字列)
  • 戻り値 : 対象ファイルの「最終更新日時」をDate型(日時)で返す。ファイルが存在しないとエラー。

使用例

ファイルの更新日時を取得する

Sub Sample()
    Dim path As String
    path = "C:\Temp\sample.txt"
    
    If Dir(path) <> "" Then
        Dim dt As Date
        dt = FileDateTime(path)
        Debug.Print "最終更新日時:" & dt
    Else
        Debug.Print "ファイルが存在しません"
    End If
End Sub

▶ 出力結果

最終更新日時:2025/08/06 10:08:21

ファイル一覧と更新日時をシートに出力

Sub Sample()
    Dim folder As String: folder = "C:\Temp\"
    Dim file As String: file = Dir(folder & "*.*")
    Dim row As Long: row = 2

    Cells(1, 1).Value = "ファイル名"
    Cells(1, 2).Value = "更新日時"
    
    Do While file <> ""
        Cells(row, 1).Value = file
        Cells(row, 2).Value = FileDateTime(folder & file)
        row = row + 1
        file = Dir()
    Loop
End Sub

▶ 出力結果
sample02_1.png
sample02_2.png

更新から7日以上経過したファイルを削除候補にする

Sub Sample()
    Dim path As String
    path = "C:\Temp\oldfile.txt"

    If Dir(path) <> "" Then
        If Now - FileDateTime(path) >= 7 Then
            MsgBox "7日以上経過しています。削除候補です。"
        Else
            MsgBox "最近更新されたファイルです。"
        End If
    End If
End Sub

▶ 出力結果(更新から7日以上経過している場合)

7日以上経過しています。削除候補です。

時刻だけを取得したい場合

以下のようにFormat関数を使用することで最終更新の時刻のみを取り出すこともできます。

Sub Sample()
    Dim path As String
    Dim lastupdate as Date
    path = "C:\Temp\sample.txt"

    If Dir(path) <> "" Then
        lastupdate = FileDateTime(path)
        Debug.Print Format(lastupdate, "hh:mm:ss")
    End If
End Sub

▶ 出力結果

10:08:21

⚠️注意

ファイルが存在しないとエラーになる

対象ファイルが存在しない場合、以下のようなエラーが発生します。

Sub Sample()
    Dim path As String
    path = "C:\Temp\sample.txt"

    Debug.Print FileDateTime(path)
End Sub

▶ 出力結果
error_53.png

エラー対策としてDir()で事前に存在確認するのが基本です。

Sub Sample()
    Dim path As String
    path = "C:\Temp\sample.txt"

    If Dir(path) <> "" Then
        Debug.Print FileDateTime(path)
    End If
End Sub

「作成日時」ではなく「最終更新日時」

FileDateTimeが取得するのは最終更新日時であり、「作成日時」や「最終アクセス日時」ではありません。

フォルダにも使用可能(ただし注意)

以下のように引数にフォルダ名までのパスを指定することで
フォルダの最終更新日時を確認することもできます。

Sub Sample()
    Dim path As String
    path = "C:\Temp"

    If Dir(path, vbDirectory) <> "" Then
        Debug.Print FileDateTime(path)
    End If
End Sub

ただし、フォルダの「更新日時」は内容の変更(ファイルの追加・削除など)でも更新されるため、意味合いが異なります。

その他のVBA関数

【Excel VBA】VBAでよく使う関数一覧&基本の使い方

参考リンク

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?