power query with VBA

svennn

New Member
Joined
Jun 11, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I want to do a power query automatic and i want to change the path of the source file.

I have recorded a macro and want to change it a bit, so the path is variable.
I still have a problem with my VBA.

VBA Code:
Sub Openen_CSV()
'
' Openen_CSV Macro
'

'

Dim Path As Variant


    Sheets("Lijst Bestanden").Select
    Path = Range("C2").Value
    
    ActiveWorkbook.Queries.Add Name:="Logging", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Bron = Table.FromColumns({Lines.FromBinary(File.Contents(""" & Path & """)), null, null, 1252)})," & Chr(13) & "" & Chr(10) & "    #""Kolom splitsen op scheidingsteken"" = Table.SplitColumn(Bron, ""Column1"", Splitter.SplitTextByDelimiter("","", QuoteStyle.None), {""Column1.1"", ""Column1.2"", ""Colu" & _
        "mn1.3"", ""Column1.4"", ""Column1.5"", ""Column1.6"", ""Column1.7"", ""Column1.8""})," & Chr(13) & "" & Chr(10) & "    #""Type gewijzigd"" = Table.TransformColumnTypes(#""Kolom splitsen op scheidingsteken"",{{""Column1.1"", type text}, {""Column1.2"", type text}, {""Column1.3"", type text}, {""Column1.4"", type text}, {""Column1.5"", type text}, {""Column1.6"", type text}, {""Column1.7"", type" & _
        " text}, {""Column1.8"", type text}})," & Chr(13) & "" & Chr(10) & "    #""Kolommen verwijderd"" = Table.RemoveColumns(#""Type gewijzigd"",{""Column1.1"", ""Column1.8""})," & Chr(13) & "" & Chr(10) & "    #""Headers met verhoogd niveau"" = Table.PromoteHeaders(#""Kolommen verwijderd"", [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Type gewijzigd1"" = Table.TransformColumnTypes(#""Headers met verhoogd niveau"",{{""Time"", Int64.Type}" & _
        ", {""Water/Algemeen"", Int64.Type}, {""Water/Buffer"", Int64.Type}, {""Temperatuur/KKWW"", Int64.Type}, {""Temperatuur/KKW"", Int64.Type}, {""Temperatuur/FT"", Int64.Type}})," & Chr(13) & "" & Chr(10) & "    #""Lege rijen verwijderd"" = Table.SelectRows(#""Type gewijzigd1"", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"""", null})))" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Lege rijen verwijderd" & _
        """"
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Logging;Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [Logging]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Logging"
        .Refresh BackgroundQuery:=False
    End With
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You recorded adding a new query and a new table tied to the query. What you must do instead is have the macro Update the existing query and then refresh:

VBA Code:
Sub Openen_CSV()
'
' Openen_CSV Macro
'

'

    Dim Path As Variant
    Dim query2Edit As WorkbookQuery
    Set query2Edit = ActiveWorkbook.Queries("Logging")
    Path = WorkSheets("Lijst Bestanden").Range("C2").Value

    query2Edit.Formula = _
    "let" & Chr(13) & "" & Chr(10) & "    Bron = Table.FromColumns({Lines.FromBinary(File.Contents(""" & Path & """)), null, null, 1252)})," & Chr(13) & "" & Chr(10) & "    #""Kolom splitsen op scheidingsteken"" = Table.SplitColumn(Bron, ""Column1"", Splitter.SplitTextByDelimiter("","", QuoteStyle.None), {""Column1.1"", ""Column1.2"", ""Colu" & _
                         "mn1.3"", ""Column1.4"", ""Column1.5"", ""Column1.6"", ""Column1.7"", ""Column1.8""})," & Chr(13) & "" & Chr(10) & "    #""Type gewijzigd"" = Table.TransformColumnTypes(#""Kolom splitsen op scheidingsteken"",{{""Column1.1"", type text}, {""Column1.2"", type text}, {""Column1.3"", type text}, {""Column1.4"", type text}, {""Column1.5"", type text}, {""Column1.6"", type text}, {""Column1.7"", type" & _
                         " text}, {""Column1.8"", type text}})," & Chr(13) & "" & Chr(10) & "    #""Kolommen verwijderd"" = Table.RemoveColumns(#""Type gewijzigd"",{""Column1.1"", ""Column1.8""})," & Chr(13) & "" & Chr(10) & "    #""Headers met verhoogd niveau"" = Table.PromoteHeaders(#""Kolommen verwijderd"", [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Type gewijzigd1"" = Table.TransformColumnTypes(#""Headers met verhoogd niveau"",{{""Time"", Int64.Type}" & _
                         ", {""Water/Algemeen"", Int64.Type}, {""Water/Buffer"", Int64.Type}, {""Temperatuur/KKWW"", Int64.Type}, {""Temperatuur/KKW"", Int64.Type}, {""Temperatuur/FT"", Int64.Type}})," & Chr(13) & "" & Chr(10) & "    #""Lege rijen verwijderd"" = Table.SelectRows(#""Type gewijzigd1"", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"""", null})))" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Lege rijen verwijderd" & _
                         """"
    ActiveWorkbook.RefreshAll
End Sub
 
Upvote 0
hello,

thank you for helping but a still get a error message that the name of the source isn't correct.

thanks!
 
Upvote 0

Forum statistics

Threads
1,223,706
Messages
6,173,998
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top