Excel VBA-运行时错误“2147217908(80040e0c)”

Excel VBA-运行时错误“2147217908(80040e0c)”,excel,vba,macos,Excel,Vba,Macos,我收到此错误:未为命令对象设置运行时错误“2147217908 80040e0c”。我是VBA新手,非常感谢您的帮助!参考其他stackoverflow错误/问题 当我点击“调试”时,它会突出显示: 开放sSQL,oCn 这里有什么问题吗 'fixing:run time error '-2147467259 automation error unspecified error Sub Unprotect_WorkSheet_With_Password() Sheets("Sheet1").U

我收到此错误:未为命令对象设置运行时错误“2147217908 80040e0c”。我是VBA新手,非常感谢您的帮助!参考其他stackoverflow错误/问题

当我点击“调试”时,它会突出显示: 开放sSQL,oCn 这里有什么问题吗

'fixing:run time error '-2147467259 automation error unspecified error

Sub Unprotect_WorkSheet_With_Password()
Sheets("Sheet1").Unprotect "YourPassword"
End Sub



    Sub Consolidate()

    Dim sSQL        As String       'SQL String
    Dim oCn         As Object       'Connection
    Dim oRs         As Object       'Recordset
    Dim vFile       As Variant      'File Name
    Dim sCustomer   As String       'Customer ID
    Dim sItem       As String       'Inventory Item ID

'   Get filenames
    vFile = Dir(ThisWorkbook.Path & "\ml\testdirectory\*.csv")

'   Create SQL
    While vFile <> vbNullString
        If sSQL <> vbNullString Then sSQL = sSQL & vbCr & "Union " & vbCr
        sCustomer = Split(vFile, "-")(0)
        sItem = Split(Split(vFile, "-")(1), ".")(0)
        sSQL = sSQL & "Select '" & sCustomer & "' as Customer, '" & sItem & "' as Item, * from [" & vFile & "]"
        vFile = Dir
        DoEvents
    Wend
'   Create Connection Objects
    Set oCn = CreateObject("ADODB.Connection")
    Set oRs = CreateObject("ADODB.Recordset")

    oCn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & ThisWorkbook.Path & ";" & _
            "Extended Properties=""Text;HDR=YES;FMT = CSVDelimited"";"
    oRs.Open sSQL, oCn
    Debug.Print sSQL

    If Sheet1.ListObjects.Count > 0 Then Sheet1.ListObjects(1).Delete
    Sheet1.ListObjects.Add( _
        SourceType:=xlSrcQuery, _
        Source:=oRs, _
        Destination:=Sheet1.Range("C6")).QueryTable.Refresh

    oRs.Close
oCn.Close

    Set oRs = Nothing
    Set oCn = Nothing

End Sub

压缩并修复accdb。确保数据库已正确关闭,用户已注销,这将处理自动错误。

什么是sSQL???@Nathan_Sav:正如代码明确指出的,这是一个字符串。Dim sSQL As String不能再解释了。它是值,当它出错时,我的意思是我猜问题在于连接字符串-我不认为CSVDelimited是有效的格式规范。请参阅条目。