简体   繁体   English

.Refresh BackgroundQuery:= False 导致 VBA 错误 1004 - Expression.Error - 源无法识别

[英].Refresh BackgroundQuery:= False causing VBA error 1004 - Expression.Error - Source not recognized

Struggling with an issue in VBA.在 VBA 中遇到问题。 The query below runs when I'm running a standard import and utilizing record macro.下面的查询在我运行标准导入并使用记录宏时运行。 However, once I added variables to the equation, I'm now getting this error when I get to the.Refresh statement.但是,一旦我将变量添加到等式中,当我进入 .Refresh 语句时,我现在会收到此错误。 I've spent a considerable amount of time researching this but I've not found a solution.我花了相当多的时间研究这个,但我还没有找到解决方案。 I've read that this could be a "false" error but not sure.我读过这可能是一个“错误”错误,但不确定。 If anyone has some insight, I would appreciate it.如果有人有一些见解,我将不胜感激。 If you need any more info, please let me know.如果您需要更多信息,请告诉我。

Dim x As String
Dim FileToOpen As String

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

'-- Generate name for query and new sheet '-- 为查询和新工作表生成名称

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

I had the same problem, and after some attempts, I discovered that if you use the code of a recorded macro, this code must not be inside a loop of any type.我也遇到了同样的问题,经过一番尝试,我发现如果你使用录制宏的代码,这段代码一定不能在任何类型的循环中。

It seems that this is not the case.似乎情况并非如此。 However, your problem might be similar, and maybe the problem is in the queries deleting part.但是,您的问题可能类似,问题可能出在查询删除部分。 Try this instead:试试这个:

Dim pq As Object For Each pq In ThisWorkbook.Queries pq.Delete Next将 pq 调暗为 Object For Each pq In ThisWorkbook.Queries pq.Delete Next

If this does not work, please give me the parts of the recorded macro that you've changed.如果这不起作用,请给我录制的宏中您已更改的部分。

Best regards,此致,

Mike麦克风

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM