首页 > 解决方案 > 预期语句结束 vba 程序

问题描述

我正在尝试将 excel 文件从不同的文件夹复制到保存/存在此脚本的文件夹中。我在下面的代码中收到“预期语句结束”错误,有人可以帮忙吗?

Option Explicit

Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder
Dim anObject As Object
sFile = "Filename.xlsm"
sSFolder = "C:\workspace"
Set FSO = CreateObject("Scripting.FileSystemObject")
sDFolder = FSO.GetAbsolutePathName("C:\") 
If Not FSO.FileExists(sSFolder & sFile) Then
    MsgBox "Specified File Not Found", vbInformation, "Not Found"
Else
    FSO.CopyFile (sSFolder & sFile), sDFolder, True
End If

标签: vba

解决方案


未经测试,但您不能使用 VBA 代码而不将其分段为子过程。您应该用 开始一个语句Sub (your subname)并用 结束它End Sub。见下文。

Option Explicit
Sub MyCode

Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder
Dim anObject As Object
sFile = "Filename.xlsm"
sSFolder = "C:\workspace"
Set FSO = CreateObject("Scripting.FileSystemObject")
sDFolder = FSO.GetAbsolutePathName("C:\") 
If Not FSO.FileExists(sSFolder & sFile) Then
    MsgBox "Specified File Not Found", vbInformation, "Not Found"
Else
    FSO.CopyFile (sSFolder & sFile), sDFolder, True
End If

End Sub

推荐阅读