VBA移动文件及移动文件夹(目录)
- 2020-02-25 08:00:00
- zstmtony 原创
- 3925
一、VBA移动文件
Public Sub VBA移动文件()
Dim strFile As String
Dim strPathNew As String
Dim fso As Scripting.FileSystemObject
strFile = ThisWorkbook.Path & "\测试文件.txt" '准备移动的文件
strPathNew = ThisWorkbook.Path & "\测试子目录\" '移动的目标位置
Set fso = New Scripting.FileSystemObject
If fso.FileExists(strFile) Then '判断是否存在
fso.MoveFile strFile, strPathNew
MsgBox "已经将文件 " & strFile & " 移到了文件夹 " & strPathNew
Else
MsgBox "要移动的文件不存在"
End If
Set fso = Nothing
End Sub
二、VBA移动文件夹
Public Sub VBA移动文件夹()
Dim strFolder As String
Dim strPathNew As String
Dim fso As Scripting.FileSystemObject
strFolder = ThisWorkbook.Path & "\待移动文件平" '要准备移动的文件夹,要先创建这个目录
strPathNew = ThisWorkbook.Path & "\测试子目录\" '移动的目标位置
Set fso = New Scripting.FileSystemObject
If fso.FolderExists(strFolder) Then '判断是否存在
fso.MoveFolder strFolder, strPathNew
MsgBox "已经将文件夹 " & strFolder & " 移到了文件夹 " & strPathNew
Else
MsgBox "要移动的文件夹不存在"
End If
Set fso = Nothing
End Sub
发表评论
文章分类
联系我们
| 联系人: | 王先生 |
|---|