其實我正在尋找代碼從一個文件夾移到Excel文件到另一個,如果有任何的方式來做到這一點,請幫助我的人。我非常抱歉,但我不知道如何編碼,因爲我從來沒有使用過VBA,事實上我第一次看到它。從一個文件夾移動到另一個文件
我會感激你
其實我正在尋找代碼從一個文件夾移到Excel文件到另一個,如果有任何的方式來做到這一點,請幫助我的人。我非常抱歉,但我不知道如何編碼,因爲我從來沒有使用過VBA,事實上我第一次看到它。從一個文件夾移動到另一個文件
我會感激你
您可以使用FileSystemObject:
Dim FSO as Object
Set FSO = CreateObject("Scripting.Filesystemobject")
FSO.MoveFile("SourceFileName", "TargetFileName")
隨意發表評論,如果您需要進一步的說明。
與下面碼
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
您必須提供完整的文件路徑作爲目標。只需帶一個文件夾,就會導致錯誤。 –
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
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
嘗試下面是從源文件夾僅移動的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
如果你需要移動只有一個文件最好的解決辦法是:
Name sourceFolderPath & fileName As destinationFilePath
可這本使用通配符使用嗎?例如,任何以xyz目錄中的「Test_」開頭並將其移動爲相同名稱的東西? – Vnge
在我的PC Excel 2013和這個代碼編輯器不接受。接受此FSO.MoveFile源:= sourceFilePath,目標:= destinationFilePath –