VBA Error when creating power query with variables

%3CLINGO-SUB%20id%3D%22lingo-sub-1320516%22%20slang%3D%22de-DE%22%3EVBA%20Error%20when%20creating%20power%20query%20with%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1320516%22%20slang%3D%22de-DE%22%3E%3CP%3EI%20have%20a%20problem%20with%20VBA.%20I%20want%20to%20create%20a%20power%20query%20and%20I%20get%20this%20error%3A%20run-time%20error%201004%2C%20application%20or%20object-defined%20error.%20I%20really%20don't%20know%20how%20to%20fix%20it%20and%20would%20really%20hope%20that%20someone%20can%20help%20me.%3C%2FP%3E%3CP%3EThis%20is%20my%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22kwd%22%3ESub%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Makro1%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E()%3C%2FSPAN%3E%0A%0A%3CSPAN%20class%3D%22kwd%22%3EDim%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20varName%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3EAs%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3EVariant%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EvarName%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Range%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22A1%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E).%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EValue%0A%0A%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3EDim%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20varSource%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3EAs%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3EVariant%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EvarSource%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Range%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22A2%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E).%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EValue%0A%3CBR%20%2F%3EActiveWorkbook%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EQueries%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EAdd%20Name%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EvarName%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Formula%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20_%0A%20%20%20%20%20%20%20%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22let%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E13%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E10%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%20%20%20%20Quelle%20%3D%20Csv.Document(File.Contents(%22%22%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20varSource%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%22%22)%2C%5BDelimiter%3D%22%22%2C%22%22%2C%20Columns%3D10%2C%20Encoding%3D1252%2C%20QuoteStyle%3DQuoteStyle.None%5D)%2C%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E13%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E10%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%20%20%20%20%23%22%22H%C3%B6her%20gestufte%20Header%22%22%20%3D%20Table.PromoteHeaders(Quelle%2C%20%5BPromoteAllScalars%3Dtrue%5D)%2C%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E13%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E10%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%20%20%20%20%23%22%22Analysierte%20JSON%22%22%20%3D%20Table.TransformColumns(%23%22%22H%C3%B6her%20gestufte%20Header%22%22%2C%7B%7B%22%22%3COPEN%3E%22%22%2C%20Json.Docum%22%3C%2FOPEN%3E%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20_%0A%20%20%20%20%20%20%20%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22ent%7D%2C%20%7B%22%22%3CHIGH%3E%22%22%2C%20Json.Document%7D%2C%20%7B%22%22%3CLOW%3E%22%22%2C%20Json.Document%7D%2C%20%7B%22%22%3CCLOSE%3E%22%22%2C%20Json.Document%7D%7D)%22%3C%2FCLOSE%3E%3C%2FLOW%3E%3C%2FHIGH%3E%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E13%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E10%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22in%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E13%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Chr%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E10%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%20%20%20%20%23%22%22Analysierte%20JSON%22%22%22%3C%2FSPAN%3E%0A%3CSPAN%20class%3D%22kwd%22%3E%3CBR%20%2F%3EWith%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20ActiveSheet%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EListObjects%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EAdd%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ESourceType%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E0%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Source%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22OLEDB%3BProvider%3DMicrosoft.Mashup.OleDb.1%3BData%20Source%3D%24Workbook%24%3BLocation%3D%22%22%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20varName%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%26amp%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22%22%22%3BExtended%20Properties%3D%22%22%22%22%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Destination%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ERange%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22%24A%241%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)).%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EQueryTable%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3CBR%20%2F%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ECommandType%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20xlCmdSql%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ECommandText%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20Array%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22SELECT%20*%20FROM%20varName%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ERowNumbers%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3EFalse%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EFillAdjacentFormulas%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3EFalse%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EPreserveFormatting%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3ETrue%3C%2FSPAN%3E%0A%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ERefreshOnFileOpen%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3EFalse%3C%2FSPAN%3E%0A%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EBackgroundQuery%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3ETrue%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ERefreshStyle%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20xlInsertDeleteCells%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ESavePassword%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3EFalse%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ESaveData%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3ETrue%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EAdjustColumnWidth%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3ETrue%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ERefreshPeriod%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3E0%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EPreserveColumnInfo%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3ETrue%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EListObject%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EName%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20varName%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ERefresh%20BackgroundQuery%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3EFalse%3C%2FSPAN%3E%0A%3CSPAN%20class%3D%22kwd%22%3E%3CBR%20%2F%3EEnd%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3EWith%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ESelection%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EListObject%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EQueryTable%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ERefresh%20BackgroundQuery%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3EFalse%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3ERange%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22L15%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E).%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3ESelect%3C%2FSPAN%3E%0A%0A%3CSPAN%20class%3D%22kwd%22%3EEnd%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3ESub%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FPRE%3E%3CP%3EIn%20this%20code%20I%20have%20two%20variables.%20Both%20are%20in%20the%20Excel%20sheet.%20Variable%20varName%20is%20the%20name%20of%20the%20query%20and%20varSource%20is%20the%20source%20of%20the%20Windows%20Explorer%20link.%3C%2FP%3E%3CP%3EThese%20are%20the%20current%20values%20in%20the%20Excel%20sheet%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E1.)%20varName%3A%20appf%20us%3C%2FP%3E%3CP%3E2.)%20varSource%3A%20C%3A%22Users%22%20-Thoma-Downloads-New%20Folder-appf.us.txt%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20think%20the%20error%20must%20be%20somewhere%20in%20the%20variable%20varName.%20Because%20when%20I%20insert%20the%20values%20of%20the%20variables%20in%20the%20code%20it%20works.%20But%20when%20I%20run%20the%20macro%20it%20creates%20the%20power%20query%20connection%20but%20does%20not%20load%20the%20data.%20When%20I%20then%20go%20to%20%22edit%20power%20query%22%20and%20look%20at%20the%20source%20I%20see%20that%20the%20source%20is%20correct.%20So%20the%20error%20has%20to%20be%20with%20varName.%3C%2FP%3E%3CP%3ESo%20when%20I%20debug%20the%20code%20it%20tells%20me%20that%20the%20error%20occurs%20in%20this%20line%3A%20.%20Refresh%20BackgroundQuery%3A%3DFalse%3C%2FP%3E%3CP%3EI%20don't%20have%20some%20other%20querys%20ore%20querys%20with%20the%20same%20name%20in%20the%20Excel%20workbook.%3C%2FP%3E%3CP%3EAnd%20this%20is%20the%20code%20of%20creating%20a%20power%20query%20that%20I%20recorded%20with%20the%20makrorecorder%20and%20without%20booth%20variables%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ESub%20Makro1()%3CBR%20%2F%3E%3CBR%20%2F%3EActiveWorkbook.Queries.Add%20Name%3A%3D%22appf%20us%22%2C%20Formula%3A%3D%20_%3CBR%20%2F%3E%22let%22%20%26amp%3B%20Chr(13)%20%26amp%3B%20%22%22%20%26amp%3B%20Chr(10)%20%26amp%3B%20%22%20Quelle%20%3D%20Csv.Document(File.Contents(%22%22C%3A%5CUsers%5CThoma%5CDownloads%5CNeuer%20Ordner%5Cappf.us.txt%22%22)%2C%5BDelimiter%3D%22%22%2C%22%22%2C%20Columns%3D10%2C%20Encoding%3D1252%2C%20QuoteStyle%3DQuoteStyle.None%5D)%2C%22%20%26amp%3B%20Chr(13)%20%26amp%3B%20%22%22%20%26amp%3B%20Chr(10)%20%26amp%3B%20%22%20%23%22%22H%C3%B6her%20gestufte%20Header%22%22%20%3D%20Table.PromoteHeaders(Quelle%2C%20%5BPromoteAllScalars%3Dtrue%5D)%2C%22%20%26amp%3B%20Chr(13)%20%26amp%3B%20%22%22%20%26amp%3B%20Chr(10)%20%26amp%3B%20%22%20%23%22%22Analysierte%20JSON%22%22%20%3D%20Table.TransformColumns(%23%22%22H%C3%B6her%20gestufte%20Header%22%22%2C%7B%7B%22%22%3COPEN%3E%22%22%2C%20Json.Docum%22%20%26amp%3B%20_%3CBR%20%2F%3E%22ent%7D%2C%20%7B%22%22%3CHIGH%3E%22%22%2C%20Json.Document%7D%2C%20%7B%22%22%3CLOW%3E%22%22%2C%20Json.Document%7D%2C%20%7B%22%22%3CCLOSE%3E%22%22%2C%20Json.Document%7D%7D)%22%20%26amp%3B%20Chr(13)%20%26amp%3B%20%22%22%20%26amp%3B%20Chr(10)%20%26amp%3B%20%22in%22%20%26amp%3B%20Chr(13)%20%26amp%3B%20%22%22%20%26amp%3B%20Chr(10)%20%26amp%3B%20%22%20%23%22%22Analysierte%20JSON%22%22%22%3CBR%20%2F%3E%3CBR%20%2F%3EWith%20ActiveSheet.ListObjects.Add(SourceType%3A%3D0%2C%20Source%3A%3D%20_%3CBR%20%2F%3E%22OLEDB%3BProvider%3DMicrosoft.Mashup.OleDb.1%3BData%20Source%3D%24Workbook%24%3BLocation%3D%22%22appf%20us%22%22%3BExtended%20Properties%3D%22%22%22%22%22%20_%3CBR%20%2F%3E%2C%20Destination%3A%3DRange(%22%24A%241%22)).QueryTable%3CBR%20%2F%3E.CommandType%20%3D%20xlCmdSql%3CBR%20%2F%3E.CommandText%20%3D%20Array(%22SELECT%20*%20FROM%20%5Bappf%20us%5D%22)%3CBR%20%2F%3E.RowNumbers%20%3D%20False%3CBR%20%2F%3E.FillAdjacentFormulas%20%3D%20False%3CBR%20%2F%3E.PreserveFormatting%20%3D%20True%3CBR%20%2F%3E.RefreshOnFileOpen%20%3D%20False%3CBR%20%2F%3E.BackgroundQuery%20%3D%20True%3CBR%20%2F%3E.RefreshStyle%20%3D%20xlInsertDeleteCells%3CBR%20%2F%3E.SavePassword%20%3D%20False%3CBR%20%2F%3E.SaveData%20%3D%20True%3CBR%20%2F%3E.AdjustColumnWidth%20%3D%20True%3CBR%20%2F%3E.RefreshPeriod%20%3D%200%3CBR%20%2F%3E.PreserveColumnInfo%20%3D%20True%3CBR%20%2F%3E.ListObject.DisplayName%20%3D%20%22appf_us%22%3CBR%20%2F%3E.Refresh%20BackgroundQuery%3A%3DFalse%3CBR%20%2F%3EEnd%20With%20Selection.ListObject.QueryTable.Refresh%20BackgroundQuery%3A%3DFalse%20Range(%22L15%22).Select%3CBR%20%2F%3EEnd%20Sub%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FCLOSE%3E%3C%2FLOW%3E%3C%2FHIGH%3E%3C%2FOPEN%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20could%20not%20add%20a%20textfile%20so%20I%20created%20this%20Google%20drive%20link%20so%20you%20can%20download%20a%20sample%20textfile%20if%20you%20want%20to%3A%20%3CA%20href%3D%22https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F1Krv2JRV88dAhTTfp4xB1nwqs1u9_q7m3%2Fview%3Fusp%3Dsharing%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F1Krv2JRV88dAhTTfp4xB1nwqs1u9_q7m3%2Fview%3Fusp%3Dsharing%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20will%20be%20appreciated!%20I%20really%20hope%20that%20someone%20has%20a%20solution%20for%20this%20problem.%3C%2FP%3E%3CP%3EThank%20you%20so%20much!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1320516%22%20slang%3D%22de-DE%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1320781%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Error%20when%20creating%20power%20query%20with%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1320781%22%20slang%3D%22en-US%22%3EDoes%20the%20code%20work%20if%20the%20query%20of%20that%20name%20is%20not%20in%20the%20workbook%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1320863%22%20slang%3D%22de-DE%22%3ERe%3A%20VBA%20Error%20when%20creating%20power%20query%20with%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1320863%22%20slang%3D%22de-DE%22%3E%3CP%3EThank%20you%20so%20much%20for%20your%20answer!%20%3CBR%20%2F%3E%3CBR%20%2F%3EI%20just%20changed%20the%20variables%20to%20this%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3EDim%20varName%20As%20Variant%0AvarName%20%3D%20%22appf%20us%22%0A%0ADim%20varSource%20As%20Variant%0AvarSource%20%3D%20%22C%3A%5CUsers%5CThoma%5CDownloads%5CNeuer%20Ordner%5Cappf.us.txt%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENo%20I%20get%20the%20same%20error%20again.%20The%20debugger%20tells%20me%2C%20that%20the%20error%20occours%20in%20this%20line%20of%20the%20complete%20code%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3E.ListObject.DisplayName%20%3D%20varName%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESo%20I%20still%20think%20that%20the%20error%20has%20to%20be%20in%20the%20variable%20%22varName%22%20but%20I%20can't%20find%20a%20solution%20to%20fix%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1320880%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Error%20when%20creating%20power%20query%20with%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1320880%22%20slang%3D%22en-US%22%3EA%20listobject's%20name%20cannot%20contain%20spaces%2C%20that%20is%20the%20source%20of%20your%20error.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1320899%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Error%20when%20creating%20power%20query%20with%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1320899%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%20for%20your%20answer!%3CBR%20%2F%3E%3CBR%20%2F%3EI%20just%20changed%20the%20varibale%20varName%20to%20this%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3EvarName%20%3D%20%22appf%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20space%20is%20now%20deleted%20but%20it%20still%20does%20not%20work.%20The%20error%20is%20also%20still%20the%20same%20but%20now%20the%20debugger%20tells%20me%20that%20the%20error%20occours%20in%20this%20line%20of%20the%20complete%20code%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3E.Refresh%20BackgroundQuery%3A%3DFalse%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1321332%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Error%20when%20creating%20power%20query%20with%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1321332%22%20slang%3D%22en-US%22%3ENow%20you've%20gotten%20to%20this%20stage%20you%20can%20go%20into%20the%20query%20and%20see%20which%20step%20causes%20an%20error.%20Stop%20the%20VBA%20macro%20first%20of%20course.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1321911%22%20slang%3D%22de-DE%22%3ERe%3A%20VBA%20Error%20when%20creating%20power%20query%20with%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1321911%22%20slang%3D%22de-DE%22%3E%3CP%3EI%20really%20appreciate%20your%20help!%20%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20data%20is%20loaded%20in%20the%20power%20query%20editor%20like%20it%20should%20be.%20It%20looks%20exactly%20the%20same%20as%20it%20looked%20when%20I%20created%20the%20power%20query%20with%20the%20editor.%20Take%20a%20look%3A%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Query%20Editor.PNG%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F185391i31E4A4E01D6B2EC8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Query%20Editor.PNG%22%20alt%3D%22Query%20Editor.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20in%20the%20Excel%20sheet%20there%20is%20no%20power%20query%20table.%20It%20just%20says%20%22ExternData_1%3A%20receiving%20Data...%22.%20Here%20is%20a%20Screenshot%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Query.PNG%22%20style%3D%22width%3A%20375px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F185390i35878308CF7C84E8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Query.PNG%22%20alt%3D%22Query.PNG%22%20%2F%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EWhen%20I%20press%20the%20data%20update%20button%20in%20Excel%20I%20get%20this%20error%3A%3C%2FP%3E%3CP%3E%22The%20import%20varName%20does%20not%20match%20an%20export.%20This%20is%20perhaps%20caused%20by%3CBR%20%2F%3E%3CBR%20%2F%3Ea%20missing%20module%20reference%22%20So%20the%20error%20has%20to%20be%20at%20the%20variable%20varName%20but%20I%20am%20still%20not%20to%20find%20a%20reason%20for%20this.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EI%20am%20sorry%20that%20this%20problem%20is%20so%20complicated%20but%20I%20really%20want%20to%20thank%20you%20for%20your%20time!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1324076%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Error%20when%20creating%20power%20query%20with%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1324076%22%20slang%3D%22en-US%22%3EWhat%20happens%20if%20you%20click%20the%20update%20button%20within%20PowerQuery%3F%20And%20are%20you%20sure%20you%20are%20looking%20at%20the%20right%20Query%3F%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I have a problem with VBA. I want to create a power query and I get this error: run-time error 1004, application or object-defined error. I really don't know how to fix it and would really hope that someone can help me.

This is my code:

 

Sub Makro1()

Dim varName As Variant
varName = Range("A1").Value Dim varSource As Variant
varSource = Range("A2").Value
ActiveWorkbook
.Queries.Add Name:=varName, Formula:= _ "let" & Chr(13) & "" & Chr(10) & " Quelle = Csv.Document(File.Contents(""" & varSource & """),[Delimiter="","", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Höher gestufte Header"" = Table.PromoteHeaders(Quelle, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Analysierte JSON"" = Table.TransformColumns(#""Höher gestufte Header"",{{""<OPEN>"", Json.Docum" & _ "ent}, {""<HIGH>"", Json.Document}, {""<LOW>"", Json.Document}, {""<CLOSE>"", Json.Document}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Analysierte JSON"""
With
ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _ "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""" & varName & """;Extended Properties=""""" _ , Destination:=Range("$A$1")).QueryTable

.
CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM varName")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.Name = varName
.Refresh BackgroundQuery:=False
End
With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Range("L15").Select End Sub


In this code I have two variables. Both are in the Excel sheet. Variable varName is the name of the query and varSource is the source of the Windows Explorer link.

These are the current values in the Excel sheet:

 

1.) varName: appf us

2.) varSource: C:\Users\Thoma\Downloads\Neuer Ordner\appf.us.txt

 

I think the error must be somewhere in the variable varName. Because when I insert the values of the variables in the code it works. But when I run the makro it creates the power query connection but does not load the data. When I then go to "edit power query" and look at the source I see that the source is correct. So the error hast to be with varName.

Also when I debug the code it tells me that the error occurs in this line: .Refresh BackgroundQuery:=False

I also don't have some other querys ore querys with the same name in the Excel workbook.

And this is the code of creating a power query that I recorded with the makrorecorder and without booth variables:

 

Sub Makro1()

ActiveWorkbook.Queries.Add Name:="appf us", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Quelle = Csv.Document(File.Contents(""C:\Users\Thoma\Downloads\Neuer Ordner\appf.us.txt""),[Delimiter="","", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Höher gestufte Header"" = Table.PromoteHeaders(Quelle, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Analysierte JSON"" = Table.TransformColumns(#""Höher gestufte Header"",{{""<OPEN>"", Json.Docum" & _
"ent}, {""<HIGH>"", Json.Document}, {""<LOW>"", Json.Document}, {""<CLOSE>"", Json.Document}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Analysierte JSON"""

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


 

I could not add a textfile so I created this Google drive link so you can download a sample textfile if you want to: https://drive.google.com/file/d/1Krv2JRV88dAhTTfp4xB1nwqs1u9_q7m3/view?usp=sharing

 

Any help will be appreciated! I really hope that someone has a solution for this problem.

Thank you so much!

7 Replies
Highlighted
Does the code work if the query of that name is not in the workbook?
Highlighted

Thank you so much for your answer!

I just changed the variables to this:

 

Dim varName As Variant
varName = "appf us"

Dim varSource As Variant
varSource = "C:\Users\Thoma\Downloads\Neuer Ordner\appf.us.txt"

 

 

No I get the same error again. The debugger tells me, that the error occours in this line of the complete code:

.ListObject.DisplayName = varName



So I still think that the error has to be in the variable "varName" but I can't find a solution to fix it.

 

Highlighted
A listobject's name cannot contain spaces, that is the source of your error.
Highlighted

Thank you for your answer!

I just changed the varibale varName to this:

varName = "appf"

 

The space is now deleted but it still does not work. The error is also still the same but now the debugger tells me that the error occours in this line of the complete code:

.Refresh BackgroundQuery:=False

 

Highlighted
Now you've gotten to this stage you can go into the query and see which step causes an error. Stop the VBA macro first of course.
Highlighted

I really appreciate your help!

The data is loaded in the power query editor like it should be. It looks exactly the same as it looked when I created the power query with the editor. Take a look:
Query Editor.PNG

 

 

But in the Excel sheet there is no power query table. It just says "ExternData_1: receiving Data...". Here is a Screenshot:

Query.PNG


When I press the data update button in Excel I get this error:

"The import varName does not match an export. This is maybe caused by an missing module reference"

So the error has to be at the variable varName but I am still not able to find a reason for this. 

I am sorry that this problem is so complicated but I really want to thank you for your time!

Highlighted
What happens if you click the update button within PowerQuery? And are you sure you are looking at the right Query?