保存工作表并删除文件按钮

时间:2014-10-13 20:30:59

标签: excel excel-vba vba

我有以下问题。我使用文件将资产(笔记本电脑,台式机等)记录到某些文件夹中,例如部署,库存,修复和热交换。

我在里面做了一些按钮,它们都很好用。一个名为已部署的按钮,当我使用此按钮保存工作表时,它将EU IMAC,序列号和日期保存为XLMS文件。

我想更改此按钮的代码,以便在我将照片保存为已部署时自动删除文件夹库存中包含序列号和名称的XLMS文件。

在所有保存按钮的代码和需要修复的按钮61下面,其他我将在之后更改。代码是其他论坛的形式,但没有成功。

Sub Button60_Click()
    Range("A1:G68").PrintOut
End Sub

Sub Button51_Click()
    ActiveWorkbook.SaveAs "C:\Users\rjbakkex\Documents\Assets_logging\Hotswap\" & Format(ActiveWorkbook.Worksheets("EU IMAC").Range("B26").Value) & " - Hotswap -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
End Sub

Sub Button53_Click()
    ActiveWorkbook.SaveAs "C:\Users\rjbakkex\Documents\Assets_logging\Returned to stock\" & Format(ActiveWorkbook.Worksheets("EU IMAC").Range("B26").Value) & " - Return to stock - " & Format(Date, "yyyy-mm-dd") & ".xlsm"
End Sub

Sub awaitwuhan_Click()
    ActiveWorkbook.SaveAs "C:\Users\rjbakkex\Documents\Assets_logging\To repair\" & Format(ActiveWorkbook.Worksheets("EU IMAC").Range("B26").Value) & "- Repair -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
End Sub

Sub Button61_Click()
    p = "C:\Users\rjbakkex\Documents\Assets_logging\Deployed\"

    'opslaan
    s_name = Sheets("EU IMAC").Range("B25").Value & " - Deployed -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
    ActiveWorkbook.SaveAs p & s_name

    'verwijderen
    d_name = Sheets("EU IMAC").Range("B25").Value & " - Return to stock -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
    If MsgBox("Are you sure that you want to remove " & d_name & " from the system?", vbQuestion + vbYesNo, "Sure?") = vbYes Then Kill p & d_name
End Sub

1 个答案:

答案 0 :(得分:0)

首先,给你的按钮有意义的名字,这是一个乱七八糟的混乱,试图确定button60是什么或做什么。

第二,你需要使用Microsoft Scripting Library中的文件系统对象(在excel中添加一个引用到这个dll scrrun.dll),然后你可以检查文件是否存在并删除它

Sub Button61_Click()
    p = "C:\Users\rjbakkex\Documents\Assets_logging\Deployed\"
        'opslaan
        s_name = Sheets("EU IMAC").Range("B25").Value & " - Deployed -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
        ActiveWorkbook.SaveAs p & s_name

    'verwijderen
        d_name = Sheets("EU IMAC").Range("B25").Value & " - Return to stock -" & Format(Date, "yyyy-mm-dd") & ".xlsm"
    'create the file system object
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    'make sure the file exists first
    If fso.FileExists(p & d_name) = True Then

        If MsgBox("Are you sure that you want to remove " & d_name & " from the system?", vbQuestion + vbYesNo, "Sure?") = vbYes Then
            fso.DeleteFile p & d_name, True
        End If


    End If
    'free the memory
    Set fso = Nothing
End Sub
相关问题