简体   繁体   English

如何使用自己的 VBA 代码移动 excel 文件?

[英]How can I move an excel file using its own VBA code?

I have an excel file that I need to move from one folder to another at the click of the button.我有一个 excel 文件,我需要单击按钮将其从一个文件夹移动到另一个文件夹。 Currently I have the following code.目前我有以下代码。

Private Sub btnReview_Click()
Dim FSO
Dim projectNumber As String
Dim fileSource As String
Dim fileDestination As String
projectNumber = Range("AI6").Value

    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    fileSource = ActiveWorkbook.path & "\" & ActiveWorkbook.Name

    fileDestination = getProjectFolder(projectNumber)
    '        ActiveWorkbook.Close True
    FSO.MoveFile fileSource, fileDestination
End Sub

Where getProjectFolder returns the path to the folder I want to move the file to(including the new name of the file). getProjectFolder返回我要将文件移动到的文件夹的路径(包括文件的新名称)。

This fails at the line FSO.MoveFile with error code 70, "Permission Denied".这在FSO.MoveFile行失败,错误代码为 70,“Permission Denied”。 I am certain that this is because it isn't possible to move or rename a file while it is open.我确信这是因为在打开文件时无法移动或重命名文件。 I have tried closing the folder before trying to move it, but that obviously ends the sub without executing any lines after the ActiveWorkbooks.Close .我在尝试移动它之前尝试关闭文件夹,但这显然结束了 sub 而没有在ActiveWorkbooks.Close之后执行任何行。

Is there any way to achieve what I am trying to do without using some external file/code?有没有办法在使用一些外部文件/代码的情况下实现我想要做的事情? I want the user to be able to push a button on a sheet, and then that sheet is moved to a separate folder.我希望用户能够按下工作表上的按钮,然后将该工作表移动到单独的文件夹中。 Is this possible?这可能吗? The only other ideas that I've had is to use the SaveAs method to save the file to the correct location and then delete the original file from the original folder.我唯一的其他想法是使用 SaveAs 方法将文件保存到正确的位置,然后从原始文件夹中删除原始文件。 That seems to be a rather inelegant solution and I don't know how that would even be possible.这似乎是一个相当不雅的解决方案,我不知道这怎么可能。

Any thoughts, tips, tricks, workarounds, etc. are appreciated.任何想法、提示、技巧、解决方法等都将受到赞赏。

Easily done!轻松搞定! Try as below, you can save it in a new folder and then proceed to delete the old file.尝试如下,您可以将其保存在新文件夹中,然后继续删除旧文件。

    Public Sub MoveMeToANewWorld()
        currentName = ThisWorkbook.Name,
        currentPathAndName = ThisWorkbook.FullName 
        currentPath = Replace(currentPathAndName, currentName, "")
        newPath = "NewFolder\"
        
    With ThisWorkbook
        .SaveAs Filename:= currentPath & newPath & currentName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    End with

'Deleting the OldFile
    With New FileSystemObject
        If .FileExists(currentPathAndName) Then
            .DeleteFile currentPathAndName 
        End If
    End With
    
    End Sub

Hope it helps you out!希望它可以帮助你!

Based on Hiran Travassos's answer I shortened his code and made it more specific to the question.根据 Hiran Travassos 的回答,我缩短了他的代码并使其更具体地解决了这个问题。

Public Sub moveMyself()
    Dim oldFile As String
    Dim projectNumber As String
    oldFile = ThisWorkbook.FullName
    projectNumber = Range("AI6").Value

    ThisWorkbook.SaveAs getProjectFolder(projectNumber), 52
    
    'Deleting the old file
    With New FileSystemObject
        .DeleteFile oldFile
    End With

End Sub

Also there is a trick you can use to continue executing code even after the original workbook that started the code got closed by abusing Application.OnTime , but it's a bit finicky.即使在启动代码的原始工作簿因滥用Application.OnTime而关闭之后,您也可以使用一个技巧继续执行代码,但这有点挑剔。

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

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