title: Import CSV with power query vba
body: <p>I would like the user to select a csv file and import the file using power query. I'm stuck and have difficulties to find the answer I'm looking for. This is what I have so far:</p>
  
  <pre><code>Dim importPathVar1, importPathVar2 As Variant
  Dim Filename As String
  
  i = 1
  
  With Application.FileDialog(msoFileDialogFilePicker)
      .AllowMultiSelect = True
      .Filters.Add "Excel Files", "*.csv", 1
      If .Show = True Then
          For Each importPathVar1 In .SelectedItems
              If i &gt; 1 Then
                  MsgBox "You selected to many files. Please select maximum 1 file."
                  Exit Sub
  
              End If
  
              If i = 1 Then
                  importPathVar2 = importPathVar1
  
              End If
  
              i = i + 1
  
          Next
  
       Else
          Exit Sub
  
       End If
  
  End With
  
  Filename = Dir(importPathVar2)
  
  ActiveWorkbook.Queries.Add Name:= _
      Filename, Formula:= _
      "let" &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "    Source = Csv.Document(File.Contents(""" &amp; importPathVar2 &amp; """),[Delimiter=""    "", Columns=37, Encoding=1252, QuoteStyle=QuoteStyle.None])," &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "    #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "    #""Changed Type"" = Table.Transfo" &amp; _
      "rmColumnTypes(#""Promoted Headers"",{{""reference"", Int64.Type}, {""ship_date_value"", type date}, {""carrier_name"", type text}, {""service_name"", type text}, {""carrier_shipment_id"", type text}, {""consignee_id"", Int64.Type}, {""prod"", type text}, {""ship_to_name"", type text}, {""ship_to_address1"", type text}, {""ship_to_address2"", type text}, {""ship_to_a" &amp; _
      "ddress3"", type text}, {""ship_to_city"", type text}, {""ship_to_state"", type text}, {""ship_to_postal_code"", type text}, {""ship_to_country_id"", type text}, {""service_def_code"", type text}, {""fq_arrive_date_value"", type date}, {""wms_origin"", type text}, {""est_del_date_value"", type date}, {""est_del_time_value"", type time}, {""total_packages_count"", Int" &amp; _
      "64.Type}, {""consolidated_to"", Int64.Type}, {""seqnum"", Int64.Type}, {""package_reference"", Int64.Type}, {""tracking_number"", type text}, {""package_sort"", type text}, {""performance_result"", type text}, {""original_order_ref"", Int64.Type}, {""tracking_status_code"", type text}, {""tracking_status_message"", type text}, {""manifest_transmit_date_value"", type" &amp; _
      " date}, {""actual_delivery_date"", type date}, {""first_tracking_exception_message"", type text}, {""last_tracking_exception_message"", type text}, {""sub_orderid"", Int64.Type}, {""transit_days"", Int64.Type}, {""actual_delivery_time"", type time}})" &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "in" &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "    #""Changed Type"""
  ActiveWorkbook.Worksheets.Add
  With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
      "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location= " &amp; Filename &amp; ";Extended Proper" _
      , "ties="""""), Destination:=Range("$A$1")).QueryTable
      .CommandType = xlCmdSql
      .CommandText = Array( _
      "SELECT * FROM [" &amp; Filename &amp; "]")
      .RowNumbers = False
      .FillAdjacentFormulas = False
      .PreserveFormatting = True
      .RefreshOnFileOpen = False
      .BackgroundQuery = True
      .RefreshStyle = xlInsertDeleteCells
      .SavePassword = False
      .SaveData = True
      .AdjustColumnWidth = True
      .RefreshPeriod = 0
      .PreserveColumnInfo = True
      .ListObject.DisplayName = Filename
      .Refresh BackgroundQuery:=False
  End With
  Application.CommandBars("Queries and Connections").Visible = False
  </code></pre>
  
  <p>I'm getting an invalid character error. This is the file name I selected "PerformanceExceptionReportKnapp_20191009152409.csv". I would very much appriciate if someone could point me in the right direction.</p>