Eliminar un archivo con una macro Excel.

Saludos
Como puedo hacer una macro programada que me elimine un archivo (Libro) de excel del disco duro.
Se que existe un método llamado DeleteFile pero no se como usarlo en una macro.

1 Respuesta

Respuesta
1
Primero que todo, disculpa la demora.
Te recomiendo revises esta página donde hay varios ejemplos sobre la manera de manejar archivos desde VBA:
http://www.tek-tips.com/faqs.cfm?fid=4116
Para eliminar un archivo está este:
Sub FileExists()
'Primero se verifica que el archivo exista
Dim fso
Dim file As String
file = "C:\Test.xls" ' change to match the file w/Path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(file) Then
MsgBox file & " was not located.", vbInformation, "File Not Found"
Else
MsgBox file & " has been located.", vbInformation, "File Found"
End If
End Sub
Sub DeleteFile()
'Luego se procede a borrarlo
Dim fso
Dim file As String
file = "C:\test.xls" ' change to match the file w/Path
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file) Then
fso.DeleteFile file, True
Else
MsgBox file & " does not exist or has already been deleted!" _
, vbExclamation, "File not Found"
End If
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas