简体   繁体   中英

Run-time error '1004':[ Expression.Error] The name 'Source' wasn't recognized. Make sure it's spelled correctly

I'm trying to add Power Query code to VBA but I'm getting an error that the "Source" wasn't recognized.

The name 'Source' wasn't recognized. Make sure it's spelled correctly

I've been trying to do different spellings or playing with the commas but I'm still getting that error. Would need a second pair of eyes and any suggestions would be extremely helpful. Thanks.

    ActiveSheet.ListObjects("Table1").TableStyle = ""
ActiveWorkbook.Queries.Add Name:="Table1", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.CurrentWorkbook(){[Name=""Table1""]}[Content]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Source, {{Table.ColumnNames(Table1){0}, " & _
    "Int64.Type}, { Table.ColumnNames(Table1){1}, type text}, { Table.ColumnNames(Table1){2}, type text}, { Table.ColumnNames(Table1){3}, type text}, { Table.ColumnNames(Table1){4}, type text}, { Table.ColumnNames(Table1){5}, type text}, { Table.ColumnNames(Table1){6}, Int64.Type}, { Table.ColumnNames(Table1){7}, " & _
    "type datetime}, { Table.ColumnNames(Table1){8}, type number}, { Table.ColumnNames(Table1){9}, type number}, { Table.ColumnNames(Table1){10}, type number}, { Table.ColumnNames(Table1){11}, type number}, { Table.ColumnNames(Table1){12}, type any}, { Table.ColumnNames(Table1){13}, type any}, { Table.ColumnNames(Table1){14}, type number}, { Table.ColumnNames(Table1){15}, type number}, { Table.ColumnNames(Table1){16}, type any}, { Table.ColumnNames(Table1){17}, type any}, { Table.ColumnNames(Table1){18}, " & _
    "type any},{ Table.ColumnNames(Table1){19}, type any}, { Table.ColumnNames(Table1){20} , type any}, { Table.ColumnNames(Table1){21}, type any}, { Table.ColumnNames(Table1){22}, type number}, { Table.ColumnNames(Table1){23}, type any}, { Table.ColumnNames(Table1){24}, type number}, { Table.ColumnNames(Table1){25}, type number}, { Table.ColumnNames(Table1){25}, type text}, { Table.ColumnNames(Table1){26}, type text}, { Table.ColumnNames(Table1){27}, type any}, { Table.ColumnNames(Table1){28}, type any}, { Table.ColumnNames(Table1){29}, " & _
    "type any}, { Table.ColumnNames(Table1){30}, type any}, { Table.ColumnNames(Table1){31}, type any}, { Table.ColumnNames(Table1){32}, {Table.ColumnNames(Table1){33} ,type any}, {Table.ColumnNames(Table1){34}, type any}, { Table.ColumnNames(Table1){35}, type any}, { Table.ColumnNames(Table1){36}, type any}, { Table.ColumnNames(Table1){37}, type any}, { Table.ColumnNames(Table1){38}, type any}, { Table.ColumnNames(Table1){39}, type any}, { Table.ColumnNames(Table1){40}, type number}, { Table.ColumnNames(Table1){41}, " & _
    "type number}, { Table.ColumnNames(Table1){42}, type number}, {Table.ColumnNames(Table1){43}, type any}, { Table.ColumnNames(Table1){44}, type any}, { Table.ColumnNames(Table1){45}, type number}, { Table.ColumnNames(Table1){46}, Int64.Type}, { Table.ColumnNames(Table1){47}, type number}, { Table.ColumnNames(Table1){48}, type any}, { Table.ColumnNames(Table1){49}, Int64.Type}, { Table.ColumnNames(Table1){50}, type any}, { Table.ColumnNames(Table1){51}, type number}, { Table.ColumnNames(Table1){52}, type text}, { Table.ColumnNames(Table1){53}, " & _
    "type text}, { Table.ColumnNames(Table1){54}, type number}, { Table.ColumnNames(Table1){55}, type text}, { Table.ColumnNames(Table1){56}, type text}, { Table.ColumnNames(Table1){57}, type any}, { Table.ColumnNames(Table1){58}, type any}, { Table.ColumnNames(Table1){59} , type any}, { Table.ColumnNames(Table1){60}, type any}, { Table.ColumnNames(Table1){61}, type any}, { Table.ColumnNames(Table1){62}, type any}, { Table.ColumnNames(Table1){63}, type any}, { Table.ColumnNames(Table1){64}, type any}, { Table.ColumnNames(Table1){65}, " & _
    "type any},  { Table.ColumnNames(Table1){66}, type number}, { Table.ColumnNames(Table1){67}, type number}, { Table.ColumnNames(Table1){68}, type number}, { Table.ColumnNames(Table1){69}, type number}, { Table.ColumnNames(Table1){70}, type number}, { Table.ColumnNames(Table1){71}, type number}, { Table.ColumnNames(Table1){72}, type number}, { Table.ColumnNames(Table1){73}, type any}, { Table.ColumnNames(Table1){74}, type any}, { Table.ColumnNames(Table1){75}, type number}, { Table.ColumnNames(Table1){76}, type number}, { Table.ColumnNames(Table1){77}, " & _
    "Int64.Type}, { Table.ColumnNames(Table1){78}, type any}, { Table.ColumnNames(Table1){79}, Int64.Type}, { Table.ColumnNames(Table1){80}, type number}, { Table.ColumnNames(Table1){81}, type any}, { Table.ColumnNames(Table1){82}, type any}, { Table.ColumnNames(Table1){83}, type any}, { Table.ColumnNames(Table1){84} , type any}, { Table.ColumnNames(Table1){85}, Int64.Type}, { Table.ColumnNames(Table1){86}, type text}, { Table.ColumnNames(Table1){87}, type any}, { Table.ColumnNames(Table1){88}, type any}, { Table.ColumnNames(Table1){89}, " & _
    "type any}, { Table.ColumnNames(Table1){90}, type number}, { Table.ColumnNames(Table1){91}, type any}, { Table.ColumnNames(Table1){92}, type any}, { Table.ColumnNames(Table1){93}, type any}, { Table.ColumnNames(Table1){94}, type any}, { Table.ColumnNames(Table1){95}, type number}, { Table.ColumnNames(Table1){96}, type any}, { Table.ColumnNames(Table1){97}, type any}, { Table.ColumnNames(Table1){98}, type any}, { Table.ColumnNames(Table1){99}, type any}, { Table.ColumnNames(Table1){100}, type any}, { Table.ColumnNames(Table1){101}, " & _
    "type any}, { Table.ColumnNames(Table1){102}, type any}, { Table.ColumnNames(Table1){103}, type any}, { Table.ColumnNames(Table1){104}, type any}, { Table.ColumnNames(Table1){105}, type any}, { Table.ColumnNames(Table1){106}, type any}, { Table.ColumnNames(Table1){107}, " & _
    "type number},  {Table.ColumnNames(Table1){108}, type any}})," & Chr(13) & "" & Chr(10) & "    #""Unpivoted Other Columns"" = Table.UnpivotOtherColumns(#""Changed Type"", {""DPT "", ""DPT-DESC                 "", ""EMP-CD"", ""FIRST NAME          "", ""LAST NAME           "", ""S.S.N.     "", ""PAY-DT"", ""Pay Date"", ""HR-RATE ""}, ""Attribute"", ""Value"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Unpivoted Other Columns"""

Try to remove any symbols between let and Source keywords and the string is well-formed excluding any extra symbols from it. The following example shows how to add a query to a workbook from an existing CSV file.

Dim mFormula As String
mFormula = _
"let Source = Csv.Document(File.Contents(""C:\data.txt""),null,""#(tab)"",null,1252) in Source"
query1 = ActiveWorkbook.Queries.Add("query1", mFormula)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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