Excel VBA - 从静态文件夹名称复制文件并粘贴到 ActiveCell 中的文件夹名称中

Zstorm123

我无法将 Excel 中 VBA 中的目标文件夹名称设置为我选择的单元格中的特定路径值。也就是说,我希望始终将在静态位置找到的特定文件复制到目标文件夹中,该文件夹因我用光标选择的单元格而异。

这是对之前发布的 sub 的修改:

Sub sbCopyingAFileReadFromSheet()
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String


'source file
sFile = Sheets("Main").Range("C26")
'source folder
SFolder = Sheets("Main").Range("c27")

'destination folder
sDFolder = Sheets("Main").ActiveCell.Value 
''' this is not working here.  I would like the destination folder 
''' to be where I have my cursor but I get 
''' an error : Object doesn't support this property or method '''


'Create Object for File System

Set FSO = CreateObject("Scripting.FileSystemObject")

'Checking If File Is Located in the Source Folder

If Not FSO.FileExists(sSFolder & sFile) Then
    MsgBox "Specified File Not Found in Source Folder", vbInformation, "Not Found"

'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
    FSO.CopyFile (sSFolder & sFile), (sSFolder & sDFolder), True
    MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
Else
    MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If

End Sub
由纪

你可以Selection用来替换Sheets("Main").ActiveCell.Value. 请尝试如下 VBA 代码:

Sub sbCopyingAFileReadFromSheet()
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
'source file
sFile = Sheets("Main").Range("C26")
'source folder
SFolder = Sheets("Main").Range("c27")
'destination folder
sDFolder = Selection
'Create Object for File System
Set FSO = CreateObject("Scripting.FileSystemObject")
'Checking If File Is Located in the Source Folder
If Not FSO.FileExists(sSFolder & sFile) Then
    MsgBox "Specified File Not Found in Source Folder", vbInformation, "Not Found"
'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
    FSO.CopyFile (sSFolder & sFile), (sSFolder & sDFolder), True
    MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
Else
    MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation,"File Already Exists"
End If
End Sub

希望对你有帮助。期待您的回音。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Excel VBA - 遍历文件夹中的文件,复制范围,粘贴到此工作簿中

从excel文件vba循环文件夹名称

VBA复制将所有文件粘贴到文件夹中

Excel VBA:文件夹名称未知但扩展名已知的文件夹路径

删除文件夹VBA中的非Excel文件

删除文件,然后在Excel 2010 VBA中的文件夹

使用VBA获取文件夹中的Excel文件列表

Excel VBA 在文件夹中找不到 xlsx 文件

Excel VBA从文件夹中的文件更新主列表

Excel VBA忽略目录中的特定文件夹/文件

从文件夹和子文件夹中获取文件列表 Excel VBA

文件夹选择器 Excel VBA 并将路径粘贴到单元格

VBA-在文件夹中打开文件并打印名称

在文件夹中搜索文件夹 (Excel-VBA)

在Excel VBA中创建文件夹和子文件夹

在VBA Excel中动态创建文件夹

VBA在字段中输出先前的文件夹名称

循环浏览子文件夹中的 Excel 文件并将数据复制并粘贴到一张纸上

使用R将文件复制粘贴到名称匹配的文件夹中

Excel VBA代码将文件夹中的rtf文件批量转换为pdf文件

Excel VBA 搜索子文件夹

Excel VBA:删除文件夹中的任何空Excel文件

VBA Excel将文件保存在Excel宏创建的文件夹中

VBA - Excel - 在文件夹中的多个文件中搜索多个字符串

如何在VBA中打开具有特定名称的文件夹中的文件?

使用excel VBA将文件自动分类到文件夹中

使用Excel VBA获取文件夹/目录中的文件名列表

使用excel vba合并文件夹中的所有pdf文件

Excel VBA如何在当前目录的文件夹中执行BAT文件