Killステートメントでフアイルを削除

 

Sub test()
 ’ Killステートメントでフアイルを削除

 

  Dim filePath As String
  filePath = ThisWorkbook.Path & "\"
  
  Kill filePath & "test_20240214.txt"


End Sub

 

Sub test1B()
 ’ Killステートメントでフアイルを削除

 

  Dim filePath As String
  filePath = ThisWorkbook.Path & "\"
  

' ワイルドカード(*)で同じ拡張子のファイル一括削除
  Kill filePath & "*.txt"


End Sub

 

 

 

 

Sub test2()
 ’ Killステートメントでフアイルを削除

 

    Dim filePath As String
    filePath = ThisWorkbook.Path & "\"
    'テストファイルを準備(事前作成のMemo.txtをコピーする)
    FileCopy filePath & "\Memo.txt", filePath & "\test_20240214.txt"
    

   ’ ファイル存在を確認する
    If Dir(filePath & "\test_20240214.txt") <> "" Then
        MsgBox "対象ファイルは存在します"
        Kill filePath & "\test_20240214.txt"
    End If


End Sub

 

 

― ― ― ― ― ― ― ― ― ― → Excel VBA基礎入門もくじ へ戻る 

 

参考記事