为VBA的一个问题而挣扎。下面的查询是在运行标准导入和使用记录宏时运行的。但是,一旦我将变量添加到方程中,当我到达.Refresh语句时,就会得到这个错误。我花了大量的时间研究这个问题,但我还没有找到解决办法。我读过这可能是一个“错误”的错误,但不确定。如果有人有洞察力,我会很感激的。如果您需要更多的信息,请告诉我。
Dim x As String
Dim FileToOpen As String
FileToOpen = Application.GetOpenFilename '-- Name of file to import
--为查询和新工作表生成名称
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
发布于 2022-09-01 09:39:01
我也遇到了同样的问题,经过几次尝试后,我发现如果使用已记录的宏的代码,则这段代码不能在任何类型的循环中。
情况似乎并非如此。但是,您的问题可能类似,问题可能在于删除部分的查询。试一试:
Dim pq作为ThisWorkbook.Queries pq.Delete Next中每个pq的对象
如果这不起作用,请给我的部分录制宏,你已经改变了。
诚挚的问候,
麦克
https://stackoverflow.com/questions/73492227
复制相似问题