nnsea 发表于 2023-1-13 22:28:23

移动指定文件到某个目录中vba


Sub 移动文件()
    Dim OldName As String
    Dim NewName As String
    OldName = ThisWorkbook.Path & "\VBA其实很简单.xlsm"    '原文件名
    NewName = ThisWorkbook.Path & "\目标目录\VBA其实很简单.xlsm"   '新文件名
    Name OldName As NewName      '不更改文件名,但将其移动到另外一个文件夹
    MsgBox "文件已经被移动了"
End Sub


Sub 移动文件()
    Dim myFile As String
    Dim myNewFilePath As String
    Dim fso As Scripting.FileSystemObject
    myFile = ThisWorkbook.Path & "\VBA其实很简单.xlsm"    '要移动的文件
    myNewFilePath = ThisWorkbook.Path & "\目标目录\"    '要移动的位置
    Set fso = New Scripting.FileSystemObject
    If fso.FileExists(myFile) Then
      fso.MoveFile myFile, myNewFilePath
      MsgBox "已经将文件 " & myFile & " 移到了文件夹 " & myNewFilePath
    Else
      MsgBox "要移动的文件不存在"
    End If
    Set fso = Nothing
End Sub
页: [1]
查看完整版本: 移动指定文件到某个目录中vba