首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >.Refresh BackgroundQuery:= False导致VBA错误1004 - Expression.Error -源未被识别

.Refresh BackgroundQuery:= False导致VBA错误1004 - Expression.Error -源未被识别
EN

Stack Overflow用户
提问于 2022-08-25 18:31:32
回答 1查看 68关注 0票数 0

为VBA的一个问题而挣扎。下面的查询是在运行标准导入和使用记录宏时运行的。但是,一旦我将变量添加到方程中,当我到达.Refresh语句时,就会得到这个错误。我花了大量的时间研究这个问题,但我还没有找到解决办法。我读过这可能是一个“错误”的错误,但不确定。如果有人有洞察力,我会很感激的。如果您需要更多的信息,请告诉我。

代码语言:javascript
运行
复制
Dim x As String
Dim FileToOpen As String

FileToOpen = Application.GetOpenFilename   '-- Name of file to import

--为查询和新工作表生成名称

代码语言:javascript
运行
复制
x = "SAP_Data_asof_" & Mid(FileToOpen, InStrRev(FileToOpen, " ") + 1, 256)
x = Left(x, InStr(x, ".") - 1)
If x = ActiveWorkbook.Queries(x) Then
    ActiveWorkbook.Queries(x).Delete
End If

ActiveWorkbook.Queries.Add Name:=x, Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(File.Contents(""" & FileToOpen & """, null, true)," & Chr(13) & "" & Chr(10) & "    Sheet1_Sheet = Source{[Item=""Sheet1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#" & _
    """Promoted Headers"",{{""Org Unit"", Int64.Type}, {""Org Unit Abbr"", type text}, {""Org Unit Text"", type text}, {""Validity Date"", type date}, {""Position"", Int64.Type}, {""Position Abbr"", type text}, {""Position Text"", type text}, {""CC OR"", type text}, {""Job Level"", type text}, {""Cost Center"", Int64.Type}, {""WFC Identifier"", type text}, {""Cost Center" & _
    " Text"", type text}, {""PersNo"", Int64.Type}, {""Employee_App First N"", type text}, {""Middle Name"", type text}, {""Last Name"", type text}, {""Known As"", type text}, {""Job Key"", Int64.Type}, {""Job Abbr"", type text}, {""Job Text"", type text}, {""Job Cat"", Int64.Type}, {""Job Cat Text"", type text}, {""Co Code"", Int64.Type}, {""Co Code Text"", type text}, " & _
    "{""Bus Area"", Int64.Type}, {""Business Area Text"", type text}, {""PA"", Int64.Type}, {""PA Text"", type text}, {""PSA"", type text}, {""PSA Text"", type text}, {""EG"", Int64.Type}, {""EG Text"", type text}, {""ESG"", Int64.Type}, {""ESG Text"", type text}, {""Chief Position"", Int64.Type}, {""Chief Title"", type text}, {""Chief Name"", type text}, {""Rpts To (A-0" & _
    "02)"", type text}, {""Reports to Title"", type text}, {""Reports to PERNR"", type text}, {""Reports to Name"", type text}, {""Takes Dir(A-DIR)"", Int64.Type}, {""Takes Direction from Title"", type text}, {""Takes Direction from PERNR"", type text}, {""Takes Direction from Name"", type text}, {""WC State"", type text}, {""WC State Text"", type text}, {""WC Code"", In" & _
    "t64.Type}, {""WC Code Text"", type text}, {""Obsolete"", type text}, {""Vacant"", type date}, {""First Name (Romaji)"", type text}, {""Last Name (Romaji)"", type text}, {""OU Func"", Int64.Type}, {""OU Function Text"", type text}})," & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns"" = Table.SelectColumns(#""Changed Type"",{""Position"", ""Position Text"", ""Job Level"", ""Cost Center" & _
    """, ""WFC Identifier"", ""Cost Center Text"", ""Last Name"", ""Known As"", ""Chief Name""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns"""


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

回答 1

Stack Overflow用户

发布于 2022-09-01 09:39:01

我也遇到了同样的问题,经过几次尝试后,我发现如果使用已记录的宏的代码,则这段代码不能在任何类型的循环中。

情况似乎并非如此。但是,您的问题可能类似,问题可能在于删除部分的查询。试一试:

Dim pq作为ThisWorkbook.Queries pq.Delete Next中每个pq的对象

如果这不起作用,请给我的部分录制宏,你已经改变了。

诚挚的问候,

麦克

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73492227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档