我正在尝试从文件夹复制文件并将其粘贴到另一个文件。因此,FileCopy函数似乎是一个很好的解决方案。但是,我要复制的文件每天都会更改名称,因此我在代码中将其命名为value3,并且该名称跟随工作簿中的单元格。
但是,在代码的最后一行出现错误(找不到文件,错误53):
FileCopy myFPName, myNewDir & value3
Sub copytxtfileinfolder()
Dim myFPName As String
Dim myNewDir As String
Dim value3 As String
value3 = Worksheets("Offset Helper Sheet").Range("B29").Value
' Full name and path of original file
myFPName = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\" & " value3"
' Name of new directory to copy to
myNewDir = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Payments Holidays\Payment Holidays txt\"
' Copy file to new directory with same name
FileCopy myFPName, myNewDir & value3
End Sub
您的问题在于此行:
myFPName = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\" & " value3"
假设value3拥有File.txt之类的内容,则不需要括号。如果用括号将其括起来,则它正在寻找的文件是G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\value3
(Excel自动删除初始空间;取而代之的是value3的值,它实际上是在寻找一个名为value3的文件)(已编辑)
更正:
myFPName = "G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Pepper sync\" & value3