Help converting Power Query that imports data to VBA

The Shy Retirer

New Member
Joined
Jan 11, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I've got a large excel model and I start off by importing the data from a csv file via VBA. As some of my data has leading 0's that I don't want to lose and I need to retain the formatting on certain fields, I've used power query and VBA to come up with the following code that imports my data and works in the way I want it to work. I won't pretend to fully understand all that is happening but I've basically used as much google as I can find to convert the query to VBA and get this far. Note that I've edited below to make it easier to read since I have 59 columns.

VBA Code:
ActiveWorkbook.Queries.Add Name:="New_Data1", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & _
    "    Filepath1 = Excel.CurrentWorkbook(){[Name=""Filepath_PolExtract""]}[Content]{0}[Column1]," & Chr(13) & "" & Chr(10) & _
    "    Source = Csv.Document(File.Contents(Filepath1),[Delimiter="","", Columns=59, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & _
    "    #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & _
    "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""PolicyNumber"", type text}, " & _
    "{""Stream"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    
    wksht3.Activate
    
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""New_Data1"";Extended Properties=""""" _
        , Destination:=Range("A1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [New_Data1]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "New_Data1"
        .Refresh BackgroundQuery:=False
    End With

This query does what I want it to do but I now want to apply a filter on one of the columns and only import what is left. I've noticed that I can filter the data in the Power Query and I'm struggling to replicate the amendment in VBA. This is the new Power Query. Again, I've deleted the majority of columns in the editor to make it easier to read, hopefully.

Power Query:
let
    Source = Csv.Document(File.Contents("L:\Policy.csv"),[Delimiter=",", Columns=59, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"PolicyNumber", type text}, {"Stream", type text}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Stream] = "A - B"))
in
    #"Filtered Rows"

I've had a go at updating my original code above for the Filtered Rows section as per the following but its falling over unfortunately. Happy to admit that I'm not very good at VBA so I've probably got this completely wrong and its not just my syntax. Any help to correct the following code would be greatly appreciated.

VBA Code:
ActiveWorkbook.Queries.Add Name:="New_Data1", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & _
    "    Filepath1 = Excel.CurrentWorkbook(){[Name=""Filepath_PolExtract""]}[Content]{0}[Column1]," & Chr(13) & "" & Chr(10) & _
    "    Source = Csv.Document(File.Contents(Filepath1),[Delimiter="","", Columns=59, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & _
    "    #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & _
    "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""PolicyNumber"", type text}, " & _
    "{""Stream"", type text}})" & Chr(13) & "" & Chr(10) & _
    "   #""Filtered Rows"" = Table.SelectRows(#""Changed Type"", each ([""Stream""] = ""A - B""))" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Filtered Rows"""
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,216,500
Messages
6,131,016
Members
449,615
Latest member
Nic0la

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