Excel 2019
I needed a power Query that imports a delimited text file and performs the same query but on different files as needed using a file picker.
I recorded the query as a macro as it does everything I need except to pass the user selected file name and other related data from the file picker to the locations indicated in the code and the Parts 1 - 6 below.
I have been able to add a file picker dilogue and added some code to the query to pass the data to it but it still does not work.
When I run the sub the file picker opens, I can choose and click on any text file but the code still opens the file that is hard coded in to the query by the recorded macro.
As you will see from the sub name, I am on revision 8 and I am out of my depth with Power Query hence the title of the post.
I attach a small workbook and text file of fake data.
From what I can tell...
Part 1.
In the code below at this line of code "ActiveWorkbook.Queries.Add Name:="Fake Test Data 1", Formula:=" the sub needs to pass the user selected file name from the file picker and replace "Fake Test Data 1" with the name of the user selected file, the file suffix is NOT REQUIRED.
Part 2.
In the code below at this line of code "Source = Csv.Document(File.Contents(""C:\Test File\Fake Test Data 1.txt"")," the sub needs to pass the full path and file name of the user selected file with suffix from the file picker.
Part 3.
In the code below at this line of code ".CommandText = Array("SELECT * FROM [Fake Test Data 1]")" ' the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED.
Part 4.
In the code below at this line of code "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Fake Test Data 1"";Extended Properties=""""" _ ' the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED.
Part 5.
In the code below at this line of code "#.ListObject.DisplayName = "Fake Test Data 1"" 'the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED and any SPACES in the file name must be replaced with a Underscore "_".
Part 6.
In the code below at this line of code "ActiveSheet.ListObjects("Fake_Test_Data_1").Unlist" 'the sub needs to pass the user selected file name from the file picker and replace "Media Pro Tools NEW" with the name of the user selected file, the file suffix is NOT REQUIRED and any SPACES in the file name must be replaced with a Underscore "_".
This is the code from the macro recorded query.
Please Login or Register to view this content.
This is the file picker and extra that I added to the query.
Please Login or Register to view this content.
This is the full sub code (such as it is) with the new code added.
Please Login or Register to view this content.
Bookmarks