一些上下文:以下宏打开指定目录中的最新文件。我试图将新打开的工作表中的所有数据复制到另一个工作表中。有时,只有有时,我会收到 1004 错误。
“工作表类的粘贴方法失败”。
有时宏有效。我无法确定为什么会发生这种情况。
任何人都可以识别代码的问题吗?清除剪贴板有时有效,但并非总是如此。此外,我一直在一个更大的宏中使用几个宏(链接到不同的文件夹)。我偶尔会遇到同样的问题。
Sub ImportOldRates()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
'Specify the path to the folder
MyPath = "C:\Folder1\Folder2\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.xls", vbNormal)
'If no files were …Run Code Online (Sandbox Code Playgroud)