0

我正在尝试运行一个宏来将访问查询结果导出到 Excel。我计划每周自动运行宏,以便将结果导出到相同的 excel 但名为日期的不同选项卡。我已经录制了一个宏来执行此操作,并尝试第二次运行该宏,但它会引发以下错误。

“错误 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
4

2 回答 2

0

如果换行会发生什么

.ListObject.DisplayName = "Table_Speed.accdb_16"

ActiveSheet.ListObjects(1).DisplayName = "Table_Speed.accdb_16"
于 2013-08-29T16:29:58.177 回答
0

改变这个:

.ListObject.DisplayName = "Table_Speed.accdb_16"

对此:

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

我相信它不喜欢 DisplayName 值中的数字

于 2022-01-04T23:16:55.110 回答