使用VBA删除文件

时间:2017-08-30 14:23:28

标签: excel vba excel-vba fso

我目前有一个删除用户表单中文件的代码。我输入一个参考,这个术语填充到一个工作表中,所有的计算都得到了解决。

我要做的是删除旧的现有文档,然后保存一个新的副本,其中包含" CLOSED"在标题中。我觉得这段代码应该可以工作,但是看不出为什么它找不到指定的文件。

Sub CloseDoc()

Dim FSO
Dim sFile As String

sFile = "M:\Documents\" & Range("B3").Text & " - " & Range("B14").Text & ".xlsx"
Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FileExists(sFile) Then
    FSO.DeleteFile sFile, True
        MsgBox "Deleted The File Successfully, Ready to update with CLOSED information", vbInformation, "Done!"
    Else
        MsgBox "Specified File Not Found", vbInformation, "Not Found!"
End If

    Sheets("ITC").Copy

    Range("A1:B54").Select
        Application.CutCopyMode = False
        Selection.Copy
        ActiveWindow.SmallScroll Down:=-60
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("A1").Select
'Save Document to Dir
    ActiveWorkbook.SaveAs Filename:= _
        "M:\Documents\" & Range("B3").Text & " - " & Range("B14").Text & " - CLOSED" & " .xlsx", FileFormat _
        :=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Close

    End Sub

0 个答案:

没有答案