+ Reply to Thread
Results 1 to 3 of 3

Prompt for file when getting data for Power Query

  1. #1
    Registered User
    Join Date
    05-05-2019
    Location
    Norcross, GA
    MS-Off Ver
    Office 365
    Posts
    1

    Prompt for file when getting data for Power Query

    I am trying to automate the process of getting data from an existing spreadsheet for use in the data model. I am able to do this as long as the file name and location are the same. I would like the VBA to allow me to select the the file rather than have the location/name hard coded.

    current code:

    Private Sub CommandButton1_Click()
    '
    ' JobLookup Import
    '
    ActiveWorkbook.Queries.Add Name:="Sheet1", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""C:\GA Retail Tax Project\Job Sales Tax Table.xlsm""), null, true)," & Chr(13) & "" & Chr(10) & " Sheet1_Sheet = Source{[Item=""Sheet1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Job"", type tex" & _
    "t}, {""State"", Int64.Type}, {""Sales Tax Code"", type text}, {""Customer"", type text}, {""Customer Name"", type text}, {""Job Name"", type text}, {""Job Address"", type text}, {""Job Address 2"", type text}, {""Job Address City"", type text}, {""Job Address Zip"", type text}, {""Date Open"", type any}, {""Contract Amount"", type text}, {""Date Closed"", type any}," & _
    " {""Sales Rep"", type any}, {""Project Manager"", type any}, {""Engineer"", type any}})," & Chr(13) & "" & Chr(10) & " #""Removed Columns"" = Table.RemoveColumns(#""Changed Type"",{""Date Closed"", ""Sales Rep"", ""Project Manager"", ""Engineer""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Removed Columns"""
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Sheet1;Extended Properties=""""" _
    , Destination:=Range("$A$4")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array("SELECT * FROM [Sheet1]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "Sheet1"
    .Refresh BackgroundQuery:=False
    End With
    End Sub

    Instead of this discrete path reference above: Source = Excel.Workbook(File.Contents(""C:\GA Retail Tax Project\Job Sales Tax Table.xlsm""), I want to include a file selection so I can pick the file from a varying network drive location. This data file will reside in a "month" folder on our server and therefore would change every month.

    Can figure out how to include this feature in the code.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: Prompt for file when getting data for Power Query

    I personally wouldn't recommend use of VBA.

    Easiest method is to use Named Range in the workbook to pass variable to PowerQuery. This is done using Advanced Editor in PowerQuery edit mode (using M query language).

    You can find details in thread below (Post #2).
    https://www.excelforum.com/excel-cha...uery-path.html

    Edit: You can set the value for the single cell named range using dropdown, formula, or using code (filesytemsobject etc).
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Prompt for file when getting data for Power Query

    Look at Application.GetOpenFilename if you still want to do that.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Power Query results to a .csv file
    By JohnnyBoyxxx in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-16-2019, 02:39 AM
  2. Replies: 6
    Last Post: 03-18-2019, 02:02 PM
  3. Replies: 0
    Last Post: 07-03-2018, 05:33 PM
  4. Sub-Forum for Excel Power Tools (Power Query, Power Pivot & Power BI)
    By chullan88 in forum Suggestions for Improvement
    Replies: 10
    Last Post: 06-28-2018, 02:25 PM
  5. [SOLVED] Excel Power Query with file being used
    By taylorsm in forum Excel General
    Replies: 34
    Last Post: 11-02-2017, 05:57 PM
  6. Replies: 11
    Last Post: 08-01-2017, 06:08 AM
  7. Power Query how do i reuse on a new file
    By thorrrr in forum Excel General
    Replies: 3
    Last Post: 05-03-2014, 02:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1