在VBA中使用幂查询将excel文件插入excel

vuktfyat  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(185)

我有一个由SAP BeX Analyzer在sharedir上生成的.xlsm文件,如果我尝试打开该文件并将其提取到另一个工作簿,我会收到错误,因为BeX是32位的,而我有64位的Office。因此,我使用powerquery,它工作正常,但有时会删除Runtime 1004表达式。语法错误:应为等于令牌。
启动.Refresh BackgroundQuery:=False时会出现此问题。
我在代码中做了一些更改-主要是变量。
我的代码:

Set wb = Workbooks.Add

wb.Queries.Add Name:="DF_GRID_1", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(File.Contents(" & Chr(34) & path & sfile & Chr(34) & "), null, true)," & Chr(13) & "" & Chr(10) & "    DF_GRID_1_DefinedName = Source{[Item=""DF_GRID_1"",Kind=""DefinedName""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(DF_GRID_1_De" & _
    "finedName, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Sending System"", type text}, {""Organizational Unit"", type text}, {""Contract Number"", type text}, {""Contract Number Description"", type text}, {""Contract Start Date"", type date}, {""Contract position"", Int64.Type}, {""Material number ID"", type" & _
    " text}, {""Material Description"", type text}, {""Start date of accept"", type date}, {""Minimum contract ter"", Int64.Type}, {""End date of previous"", type text}, {""Blacklist ID"", type text}, {""Contract position st"", Int64.Type}, {""Contract position ty"", Int64.Type}, {""Portfolio ID"", Int64.Type}, {""Business Area"", Int64.Type}, {""Distribution channel"", " & _
    "Int64.Type}, {""Material group"", Int64.Type}, {""Condition Type"", Int64.Type}, {""Date From"", type date}, {""Price duration in mo"", Int64.Type}, {""Currency"", type text}, {""Months before min. c"", type text}, {""Amortization Period"", Int64.Type}, {""Selection period"", type date}, {""Sales Quantity of contract position"", Int64.Type}, {""Value per month"", ty" & _
    "pe number}, {""SSP Value"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=DF_GRID_1;Extended Properties=""""" _
    , Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array("SELECT * FROM [DF_GRID_1]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "DF_GRID_1"
    .Refresh BackgroundQuery:=False
End With

应用程序.命令栏(“查询和连接”).Visible = False
我删除了添加新工作表,使文件和工作簿变量,表从BeX(DF_GRID_1)保留。
录制宏时的代码:

ActiveWorkbook.Queries.Add Name:="DF_GRID_1", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(File.Contents(""\\server\_Monthly closing\IFRS_CNTR_RAW_DATA.xlsm""), null, true)," & Chr(13) & "" & Chr(10) & "    DF_GRID_1_DefinedName = Source{[Item=""DF_GRID_1"",Kind=""DefinedName""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(DF_GRID_1_De" & _
    "finedName, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Sending System"", type text}, {""Organizational Unit"", type text}, {""Contract Number"", type text}, {""Contract Number Description"", type text}, {""Contract Start Date"", type date}, {""Contract position"", Int64.Type}, {""Material number ID"", type" & _
    " text}, {""Material Description"", type text}, {""Start date of accept"", type date}, {""Minimum contract ter"", Int64.Type}, {""End date of previous"", type text}, {""Blacklist ID"", type text}, {""Contract position st"", Int64.Type}, {""Contract position ty"", Int64.Type}, {""Portfolio ID"", Int64.Type}, {""Business Area"", Int64.Type}, {""Distribution channel"", " & _
    "Int64.Type}, {""Material group"", Int64.Type}, {""Condition Type"", Int64.Type}, {""Date From"", type date}, {""Price duration in mo"", Int64.Type}, {""Currency"", type text}, {""Months before min. c"", type text}, {""Amortization Period"", Int64.Type}, {""Selection period"", type date}, {""Sales Quantity of contract position"", Int64.Type}, {""Value per month"", ty" & _
    "pe number}, {""SSP Value"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=DF_GRID_1;Extended Properties=""""" _
    , Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array("SELECT * FROM [DF_GRID_1]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "DF_GRID_1"
    .Refresh BackgroundQuery:=False
End With

有什么办法可以找出什么地方出错了吗?电源查询显示相同的错误VBA。我不需要任何修改从文件,我只需要加载DF_GRID_1到工作表打开的工作簿。
无法以其他方式生成此文件-它是RPA生成的文件,供其他进程使用。我无法使用64位Office打开此文件,除非收到无法抑制的错误消息

With Application
   .ScreenUpdating = False
   .DisplayAlerts = False
   .EnableEvents = False
   .AskToUpdateLinks = False
End With
vof42yt1

vof42yt11#

似乎奥卡姆剃刀工作,在只读模式下打开.xlsm就足以抑制里面的BeX宏

Set wbs = Workbooks.Open(FileName:=path & sfile, ReadOnly:=True)

线程https://stackoverflow.com/a/3389577/17572067的荣誉

相关问题