Excel VBA - Power Query import with new sheets

%3CLINGO-SUB%20id%3D%22lingo-sub-1312857%22%20slang%3D%22de-DE%22%3EExcel%20VBA%20-%20Power%20Query%20import%20with%20new%20sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1312857%22%20slang%3D%22de-DE%22%3E%3CP%3EHello%2C%3CBR%20%2F%3EI%20have%20a%20problem%20with%20Excel%20VBA.%20I%20want%20to%20import%20several%20textfiles%20in%20Excel%20with%20Power%20Query%20(Power%20Query%20is%20needed%20because%20the%20files%20need%20to%20be%20connected%20to%20the%20folder%20after%20they%20are%20imported).%20Every%20textfile%20needs%20to%20be%20in%20a%20new%20Excel%20sheet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20VBA%20code%20should%20work%20like%20this%3A%3C%2FP%3E%3CP%3E1.)%20Open%20the%3CBR%20%2F%3EWindows%20explorer%202.)%20Select%20all%20textfiles%20that%20are%20saved%3CBR%20%2F%3Ein%20the%20choosen%20folder%203.)%20Import%20the%20first%20textfile%20in%20the%3CBR%20%2F%3Ecurren%20Exel%20sheet%204.)%20Create%20a%20new%3CBR%20%2F%3EExcel%20sheet%205.)%20Import%20the%20next%20textfile%3CBR%20%2F%3Ein%20the%20new%20sheet%206.)%20Repeat%205-6%20until%20every%20textfile%20is%20imported%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20textfiles%20I%20want%20to%20import%20have%20all%20the%20same%20structure.%20The%20columns%20are%20seperated%20by%20commas.%3C%2FP%3E%3CP%3EI%20already%20used%20the%20macro%20recorder%20where%20I%20imported%20one%20textfile%20and%20after%20that%20I%20added%20a%20new%20Excel%20sheet.%20This%20is%20the%20code%20(I%20hope%20this%20helps)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3ESub%20Makro1()%0A'%0A'%20Makro1%20Makro%0A'%0A%0A'%0AActiveWorkbook.Queries.Add%20Name%3A%3D%22aacg%20us%22%2C%20Formula%3A%3D%20_%0A%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%5Caacg.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_%0A%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%0AWith%20ActiveSheet.ListObjects.Add(SourceType%3A%3D0%2C%20Source%3A%3D%20_%0A%22OLEDB%3BProvider%3DMicrosoft.Mashup.OleDb.1%3BData%20Source%3D%24Workbook%24%3BLocation%3D%22%22aacg%20us%22%22%3BExtended%20Properties%3D%22%22%22%22%22%20_%0A%2C%20Destination%3A%3DRange(%22%24A%241%22)).QueryTable%0A.CommandType%20%3D%20xlCmdSql%0A.CommandText%20%3D%20Array(%22SELECT%20*%20FROM%20%5Baacg%20us%5D%22)%0A.RowNumbers%20%3D%20False%0A.FillAdjacentFormulas%20%3D%20False%0A.PreserveFormatting%20%3D%20True%0A.RefreshOnFileOpen%20%3D%20False%0A.BackgroundQuery%20%3D%20True%0A.RefreshStyle%20%3D%20xlInsertDeleteCells%0A.SavePassword%20%3D%20False%0A.SaveData%20%3D%20True%0A.AdjustColumnWidth%20%3D%20True%0A.RefreshPeriod%20%3D%200%0A.PreserveColumnInfo%20%3D%20True%0A.ListObject.DisplayName%20%3D%20%22aacg_us%22%0A.Refresh%20BackgroundQuery%3A%3DFalse%0AEnd%20With%0ASelection.ListObject.QueryTable.Refresh%20BackgroundQuery%3A%3DFalse%0ASheets.Add%20After%3A%3DActiveSheet%0AEnd%20Sub%3C%2FCLOSE%3E%3C%2FLOW%3E%3C%2FHIGH%3E%3C%2FOPEN%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20also%20added%20some%20sample%20data%20so%20you%20can%20see%20how%20it%20is%20structured.%3C%2FP%3E%3CP%3EThank%20you%20so%20much%20for%20your%20help!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1312857%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
Highlighted
Occasional Contributor

Hello,
I have a problem with Excel VBA. I want to import several textfiles in Excel with Power Query (Power Query is needed because the files need to be connected to the folder after they are imported). Every textfile needs to be in a new Excel sheet.

 

The VBA code should work like this:

1.) Open the Windows explorer
2.) Select all textfiles that are saved in the choosen folder
3.) Import the first textfile in the curren Exel sheet
4.) Create a new Excel sheet
5.) Import the next textfile in the new sheet
6.) Repeat 5-6 until every textfile is imported

 

The textfiles I want to import have all the same structure. The columns are seperated by commas.

I already used the makrorecorder where I imported one textfile and after that I added a new Excel sheet. This is the code (I hope this helps):

 

 

Sub Makro1()
'
' Makro1 Makro
'

'
ActiveWorkbook.Queries.Add Name:="aacg us", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Quelle = Csv.Document(File.Contents(""C:\Users\Thoma\Downloads\Neuer Ordner\aacg.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=""aacg us"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [aacg 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 = "aacg_us"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Sheets.Add After:=ActiveSheet
End Sub

 

 

I also added some sample data so you can see how it is structured.

Thank you so much for your help!

0 Replies