如果重命名,则自动删除excel文件

时间:2017-09-15 01:48:00

标签: excel excel-vba excel-2007 vba

我正在处理工资的Excel文件。我想在重命名后自动删除此Excel文件。我的意思是如果任何人在重命名后打开它,它将被自动删除。我的代码正在运行,但它也删除它而不重命名文件。

Private Sub workbook_open()
    Dim xFileName As String
    xFileName = "Salary.xlsm"
    If xFileName <> "True" Then
        Dim ws As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next ws
        Sheets("User").Visible = xlVeryHidden
        Sheets("Pass").Visible = xlVeryHidden
    End If

    If xFileName <> "False" Then
        Me.ChangeFileAccess xlReadOnly
        MsgBox "File Name Changed- iTach"
        Kill Me.FullName
        Application.Quit
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

请尝试以下代码..

        Private Sub workbook_open()
        Dim xFileName As String
        xFileName = "salary.xlsm"
        If xFileName = ThisWorkbook.Name Then
            Dim ws As Worksheet
            For Each ws In ThisWorkbook.Worksheets
            ws.Visible = xlSheetVisible
            Next ws
            Sheets("User").Visible = xlVeryHidden
            Sheets("Pass").Visible = xlVeryHidden
        Else
            Me.ChangeFileAccess xlReadOnly
            MsgBox "File Name Changed- iTach"
            Kill Me.FullName
            Application.Quit
        End If
        End Sub