【问题标题】:Copying file from one destination folder to another将文件从一个目标文件夹复制到另一个
【发布时间】:2017-11-16 10:50:14
【问题描述】:

我目前正在尝试使用 Excel VBA 宏将文件从一个文件夹复制到另一个指定文件夹,我的数据在 Excel 的表 1 上,我已将文件名设置在单元格 B5 上,源文件夹已打开B6,目标文件夹位于单元格 B7 上。这是我下面的代码:

'In this Example I am Copying the File From one loaction to another location
'as per the details specified in the Worksheet.
Sub sbCopyingAFileReadFromSheet()
'Declaration
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String

sFile = Sheets("Sheet1").Range("B5")

sSFolder = Sheets("Sheet1").Range("B6")

sDFolder = Sheets("Sheet1").Range("B7")

Set FSO = CreateObject("Scripting.FileSystemObject")

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

ElseIf Not FSO.FileExists(sDFolder & sFile) Then
FSO.CopyFile (sSFolder & sFile), 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

但即使文件位于源文件夹中,也会不断弹出“在源文件夹中未找到指定文件”错误消息。请帮忙

【问题讨论】:

  • 文件是像 *C:* 这样的本地驱动器还是像 *\\somewhere\* 这样的网络驱动器上的文件??跨度>
  • 单元格B5,B6,B7 中的值是多少?您是否在文件夹路径和文件路径之间包含"\"?文件名是带扩展名的吗?
  • 我意识到我没有在文件夹路径的末尾添加“\”。

标签: vba excel


【解决方案1】:

使用sSFolder & sFile 时,请确保两个变量之间有一个“\”,如下所示

sSFolder & "\" & sFile

【讨论】:

    【解决方案2】:

    使用路径分隔符:

    If Not FSO.FileExists(sSFolder & Application.PathSeparator & sFile) Then
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多