简体   繁体   English

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

[英]Save the worksheet and delete file button

I have the following question. 我有以下问题。 I use a file to log assets (laptops, desktops etc) into certain folders, like deployed, stock, repair and hotswap. 我使用文件将资产(笔记本电脑,台式机等)记录到某些文件夹中,例如已部署,库存,维修和热交换。

I made some buttons in it which work all fine. 我在其中做了一些按钮,一切正常。 One button called deployed, when I save the sheet with this button it saves it with EU IMAC, serial number and date as XLMS file. 一个按钮称为部署,当我使用此按钮保存工作表时,它将以EU IMAC,序列号和日期保存为XLMS文件。

I like to change the code from this button, so that when I save a sheet as deployed it automatically delete the XLMS file with serial number and name in the folder stock. 我想通过此按钮更改代码,以便在保存工作表时将其自动删除文件夹中具有序列号和名称的XLMS文件。

Below the codes for all the save buttons and it's button 61 that needs to be fixed, the others I will change afterwards. 在所有保存按钮和需要固定的按钮61的代码下方,其余的我将在以后进行更改。 The code is form other forum, but with no success. 该代码来自其他论坛,但没有成功。

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

First, give your buttons meaningful names, that is such a garbled mess to try and determine what button60 is or does. 首先,给您的按钮起一个有意义的名称,那就是一堆乱七八糟的东西,试图确定button60是什么或做什么。

Second You need to use the file system object from Microsoft Scripting Library (add a reference in excel to this dll scrrun.dll) then you can check to see if the file exists and delete it 其次,您需要使用Microsoft脚本库中的文件系统对象(将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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM