Prompt User to Select Power Query Data Source

I’d like to set up some VBA so that users are prompted to select 2 different filepaths, and those files are then set as the source in two different powerquery tables. I’d like to just set it so the powerquery code can refer to a variant defined by the VBA instead of referring to the named cell in the excel sheet. Any solutions?

My current workaround is that I have some VBA that prompts users to select select a file path and then another file path. Those filepaths are then set as two different cell values on a worksheet. I’ve named those cells (“PopManFilePath” and “ClinComFilePath”). The source in my powerquery is set as those named cells.
See VBA below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Sub FileSelectandRefreshQueries()
Dim answerpop As Integer
answerpop = MsgBox("Please select your population management file in the following window. It must be a .csv file.", vbOKCancel, "Instructions")
If answerpop = vbOK Then
Dim PopManFileName As Variant
PopManFileName = Application.GetOpenFilename(fileFilter:="CSV Files (*.csv),*.csv", Title:="Please select population management file")
If PopManFileName <> False Then
ThisWorkbook.Worksheets("FilePaths").Range("B1").Value = PopManFileName
End If
End If
Dim answerclin As Integer
answerclin = MsgBox("Please select your clinical complexity file in the following window. It must be a .csv file.", vbOKCancel, "Instructions")
If answerclin = vbOK Then
Dim ClinComName As Variant
ClinComName = Application.GetOpenFilename(fileFilter:="CSV Files (*.csv),*.csv", Title:="Please select clinical complexity file")
If ClinComName <> False Then
ThisWorkbook.Worksheets("FilePaths").Range("B2").Value = ClinComName
End If
End If
ThisWorkbook.RefreshAll
End Sub
</code>
<code>Sub FileSelectandRefreshQueries() Dim answerpop As Integer answerpop = MsgBox("Please select your population management file in the following window. It must be a .csv file.", vbOKCancel, "Instructions") If answerpop = vbOK Then Dim PopManFileName As Variant PopManFileName = Application.GetOpenFilename(fileFilter:="CSV Files (*.csv),*.csv", Title:="Please select population management file") If PopManFileName <> False Then ThisWorkbook.Worksheets("FilePaths").Range("B1").Value = PopManFileName End If End If Dim answerclin As Integer answerclin = MsgBox("Please select your clinical complexity file in the following window. It must be a .csv file.", vbOKCancel, "Instructions") If answerclin = vbOK Then Dim ClinComName As Variant ClinComName = Application.GetOpenFilename(fileFilter:="CSV Files (*.csv),*.csv", Title:="Please select clinical complexity file") If ClinComName <> False Then ThisWorkbook.Worksheets("FilePaths").Range("B2").Value = ClinComName End If End If ThisWorkbook.RefreshAll End Sub </code>
Sub FileSelectandRefreshQueries()
    Dim answerpop As Integer
    answerpop = MsgBox("Please select your population management file in the following window. It must be a .csv file.", vbOKCancel, "Instructions")
    
    If answerpop = vbOK Then
        Dim PopManFileName As Variant
         PopManFileName = Application.GetOpenFilename(fileFilter:="CSV Files (*.csv),*.csv", Title:="Please select population management file")
     
            If PopManFileName <> False Then
            ThisWorkbook.Worksheets("FilePaths").Range("B1").Value = PopManFileName
            End If
    End If
    
    Dim answerclin As Integer
    answerclin = MsgBox("Please select your clinical complexity file in the following window. It must be a .csv file.", vbOKCancel, "Instructions")
    
        If answerclin = vbOK Then
        Dim ClinComName As Variant
         ClinComName = Application.GetOpenFilename(fileFilter:="CSV Files (*.csv),*.csv", Title:="Please select clinical complexity file")
        
            If ClinComName <> False Then
            ThisWorkbook.Worksheets("FilePaths").Range("B2").Value = ClinComName
            End If
        End If

ThisWorkbook.RefreshAll

End Sub

And this is my very simple powerquery code to set the source in one of my tables:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>let
LocalPath = Excel.CurrentWorkbook(){[Name="PopManFilePath"]}[Content]{0}[Column1],
Source = Csv.Document(File.Contents(LocalPath),[Delimiter="#(tab)", Encoding=1200, QuoteStyle=QuoteStyle.None]),
</code>
<code>let LocalPath = Excel.CurrentWorkbook(){[Name="PopManFilePath"]}[Content]{0}[Column1], Source = Csv.Document(File.Contents(LocalPath),[Delimiter="#(tab)", Encoding=1200, QuoteStyle=QuoteStyle.None]), </code>
let
  LocalPath = Excel.CurrentWorkbook(){[Name="PopManFilePath"]}[Content]{0}[Column1],
  Source = Csv.Document(File.Contents(LocalPath),[Delimiter="#(tab)", Encoding=1200, QuoteStyle=QuoteStyle.None]),

I feel like there must be a better way to do this that doesn’t involve writing the file paths into a cell.

New contributor

sfass is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

The only way that I know we can avoid using an Excel Range is to update the formula.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Sub UpdateFileQuery(FilePath As String)
Const QueryName As String = "TestQuery"
Dim Query As WorkbookQuery
Set Query = ThisWorkbook.Queries(QueryName)
Query.Formula = "let" & vbNewLine & _
"Source = Folder.Files(" & Chr(34) & FilePath & Chr(34) & ")" & vbNewLine & _
"in" & vbNewLine & _
" Source"
Query.Refresh
End Sub
</code>
<code>Sub UpdateFileQuery(FilePath As String) Const QueryName As String = "TestQuery" Dim Query As WorkbookQuery Set Query = ThisWorkbook.Queries(QueryName) Query.Formula = "let" & vbNewLine & _ "Source = Folder.Files(" & Chr(34) & FilePath & Chr(34) & ")" & vbNewLine & _ "in" & vbNewLine & _ " Source" Query.Refresh End Sub </code>
Sub UpdateFileQuery(FilePath As String)
    Const QueryName As String = "TestQuery"
    Dim Query As WorkbookQuery
    Set Query = ThisWorkbook.Queries(QueryName)
    Query.Formula = "let" & vbNewLine & _
        "Source = Folder.Files(" & Chr(34) & FilePath & Chr(34) & ")" & vbNewLine & _
        "in" & vbNewLine & _
        "   Source"
    Query.Refresh
End Sub

There are many ways that you could improve the User Experience (UX).

  • Change the default directory before calling Application.GetOpenFilename
  • Eliminate the first MsgBox
  • If you already know the correct folder, populate List or Combo Boxes with the file names
  • Create a Userform to fetch the files

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật