Excel宏错误:“错误1004:应用程序违反或对象定义的错误”

时间:2013-08-29 14:44:13

标签: excel-vba ms-access-2007 vba excel

感谢您的帮助和支持。我很擅长excel,这可能是一个基本问题。

我正在尝试运行宏来将访问查询结果导出到excel。我计划自动运行宏来每周运行,因此结果应该导出到相同的excel但不同的选项卡被命名为date。我已经录制了一个宏来执行此操作并尝试第二次运行宏并且它会抛出错误。

  

“错误1004:应用程序违反或对象定义错误”

并指向此行代码.ListObject.DisplayName = "Table_Speed.accdb_16"

Sub Macro_Query3()

    Dim ws As Worksheet

    For Each ws In Worksheets
        If Left(ws.Name, 8) = Format(Now(), "mm-dd-yy") Then x = x + 1
    Next

    Sheets.Add.Name = Format(Now(), "mm-dd-yy") & " (" & x + 1 & ")"

    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
        "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\s" _
        , _
        "peed\Speed.accdb;Mode=Share Deny Write;Extended Properties="""";Jet OLEDB:System database="""";Jet " _
        , _
        "OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global" _
        , _
        " Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=Fal" _
        , _
        "se;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;" _
        , "Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False"), Destination:= _
        Range("$A$1")).QueryTable
        .CommandType = xlCmdTable
        .CommandText = Array("Final")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .SourceDataFile = _
        "C:\speed\Speed.accdb"
        .ListObject.DisplayName = "Table_Speed.accdb_16"
        .Refresh BackgroundQuery:=False
    End With
End Sub

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果更改此行会怎样?

.ListObject.DisplayName = "Table_Speed.accdb_16"

ActiveSheet.ListObjects(1).DisplayName = "Table_Speed.accdb_16"