将文件从一个文件夹移动到另一个

时间:2022-11-15 18:22:26

actually I am searching for code to move excel files from one folder to another if there is any way to do so Please someone help me. I am very sorry but I dont know how to do coding as I have never used VBA in fact I see it for the first time.

实际上我正在搜索代码将excel文件从一个文件夹移动到另一个文件夹,如果有任何方法这样做请有人帮助我。我很抱歉,但我不知道如何编码,因为我从未使用VBA,事实上我第一次看到它。

I will be grateful to you

我将感激你

5 个解决方案

#1


2  

Try with the below code

尝试使用以下代码

Sub test()
    Set fso = CreateObject("scripting.filesystemobject")
    fso.MoveFile Source:="C:\work\test1.xlsx", Destination:="c:\work\movecheck\" ' replace with source and destination as required.
End Sub

#2


2  

Sub MoveFiles()
    Dim FSO As Object
    Dim SourceFileName, DestinFileName As String

    Set FSO = CreateObject("Scripting.Filesystemobject")
    SourceFileName = "C:\Users\Jun.xlsx"
    DestinFileName = "C:\Users\Desktop\Jun.xlsx"

    FSO.MoveFile Source:=SourceFileName, Destination:=DestinFileName

    MsgBox (SourceFileName + " Moved to " + DestinFileName)
End Sub

#3


1  

You can use the Filesystemobject:

您可以使用Filesystemobject:

Dim FSO as Object
Set FSO = CreateObject("Scripting.Filesystemobject")
FSO.MoveFile("SourceFileName", "TargetFileName")

Feel free to comment, if you need further instructions.

如果您需要进一步的说明,请随时发表评论。

#4


1  

Sub move_data()
    'Move test data to folder

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim Fdate As Date
    Dim FileInFromFolder As Object


    MkDir "D:\TEST\"        'Create new folder name TEST in D:
    FromPath = "E:\test\"   'Source files
    ToPath = "D:\TEST\"     'Target destination

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If

    For Each FileInFromFolder In FSO.getfolder(FromPath).Files
        FileInFromFolder.move ToPath
    Next FileInFromFolder

End Sub

#5


1  

Below is code which moves only Excel (xlsx) files from source folder into destination folder. Other types files will be left in the destination folder.

下面是仅将Excel(xlsx)文件从源文件夹移动到目标文件夹的代码。其他类型的文件将保留在目标文件夹中。

Sub MoveFiles()

Dim sourceFolderPath As String, destinationFolderPath As String
Dim FSO As Object, sourceFolder As Object, file As Object
Dim fileName As String, sourceFilePath As String, destinationFilePath As String

Application.ScreenUpdating = False

sourceFolderPath = "D:\SourceFolder"
destinationFolderPath = "D:\DestinationFolder"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set sourceFolder = FSO.Getfolder(sourceFolderPath)

For Each file In sourceFolder.Files
    fileName = file.Name
    If InStr(fileName, ".xlsx") Then ' Only xlsx files will be moved
        sourceFilePath = file.Path
        destinationFilePath = destinationFolderPath & "\" & fileName
        FSO.MoveFile Source:=sourceFilePath, Destination:=destinationFilePath
    End If ' If InStr(sourceFileName, ".xlsx") Then' Only xlsx files will be moved
Next

'Don't need set file to nothing because it is initialized in for each loop 
'and after this loop is automatically set to Nothing
Set sourceFolder = Nothing
Set FSO = Nothing

End Sub

If you need move only one file the best solution is:

如果您只需要移动一个文件,最佳解决方案是:

Name sourceFolderPath & fileName As destinationFilePath

#1


2  

Try with the below code

尝试使用以下代码

Sub test()
    Set fso = CreateObject("scripting.filesystemobject")
    fso.MoveFile Source:="C:\work\test1.xlsx", Destination:="c:\work\movecheck\" ' replace with source and destination as required.
End Sub

#2


2  

Sub MoveFiles()
    Dim FSO As Object
    Dim SourceFileName, DestinFileName As String

    Set FSO = CreateObject("Scripting.Filesystemobject")
    SourceFileName = "C:\Users\Jun.xlsx"
    DestinFileName = "C:\Users\Desktop\Jun.xlsx"

    FSO.MoveFile Source:=SourceFileName, Destination:=DestinFileName

    MsgBox (SourceFileName + " Moved to " + DestinFileName)
End Sub

#3


1  

You can use the Filesystemobject:

您可以使用Filesystemobject:

Dim FSO as Object
Set FSO = CreateObject("Scripting.Filesystemobject")
FSO.MoveFile("SourceFileName", "TargetFileName")

Feel free to comment, if you need further instructions.

如果您需要进一步的说明,请随时发表评论。

#4


1  

Sub move_data()
    'Move test data to folder

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim Fdate As Date
    Dim FileInFromFolder As Object


    MkDir "D:\TEST\"        'Create new folder name TEST in D:
    FromPath = "E:\test\"   'Source files
    ToPath = "D:\TEST\"     'Target destination

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If

    For Each FileInFromFolder In FSO.getfolder(FromPath).Files
        FileInFromFolder.move ToPath
    Next FileInFromFolder

End Sub

#5


1  

Below is code which moves only Excel (xlsx) files from source folder into destination folder. Other types files will be left in the destination folder.

下面是仅将Excel(xlsx)文件从源文件夹移动到目标文件夹的代码。其他类型的文件将保留在目标文件夹中。

Sub MoveFiles()

Dim sourceFolderPath As String, destinationFolderPath As String
Dim FSO As Object, sourceFolder As Object, file As Object
Dim fileName As String, sourceFilePath As String, destinationFilePath As String

Application.ScreenUpdating = False

sourceFolderPath = "D:\SourceFolder"
destinationFolderPath = "D:\DestinationFolder"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set sourceFolder = FSO.Getfolder(sourceFolderPath)

For Each file In sourceFolder.Files
    fileName = file.Name
    If InStr(fileName, ".xlsx") Then ' Only xlsx files will be moved
        sourceFilePath = file.Path
        destinationFilePath = destinationFolderPath & "\" & fileName
        FSO.MoveFile Source:=sourceFilePath, Destination:=destinationFilePath
    End If ' If InStr(sourceFileName, ".xlsx") Then' Only xlsx files will be moved
Next

'Don't need set file to nothing because it is initialized in for each loop 
'and after this loop is automatically set to Nothing
Set sourceFolder = Nothing
Set FSO = Nothing

End Sub

If you need move only one file the best solution is:

如果您只需要移动一个文件,最佳解决方案是:

Name sourceFolderPath & fileName As destinationFilePath