Hello All,
I am building a report template to send to outside branches which have some not so tech savvy users. I'm trying to automate as much as I can so the end users only need to click a couple buttons that I've assigned to macros and have PQ/VBA do the rest.
Each week a report is ran that dumps data into a mess of a txt file. I am using power query to clean up the data and load it into Excel. I have a VBA script that allows the user to navigate to the txt file report and import it into the correct sheet. However, I have yet to figure out a way to then run the power query steps to clean it up after it's imported. Currently I am going into the PQ advanced editor and changing the filepath to the new report file.
Is there a way to hard code the M code into VBA and run it on the applicable worksheet? Can I have the end users save the query but have the source dynamically change to the current worksheet after the report has been imported? I would ideally like to avoid having the user save the query as I would then have to help them change the filepath as it will be a different location for every user. Below is my VBA and M code, any help would be greatly appreciated!
Quick note: I am using the same PaidHours sub to import payroll info as well, using "("Text Files (*.txt), *.txt")" as the file filter on a duplicate sub. I know the one I pasted is for excel files only.
I am building a report template to send to outside branches which have some not so tech savvy users. I'm trying to automate as much as I can so the end users only need to click a couple buttons that I've assigned to macros and have PQ/VBA do the rest.
Each week a report is ran that dumps data into a mess of a txt file. I am using power query to clean up the data and load it into Excel. I have a VBA script that allows the user to navigate to the txt file report and import it into the correct sheet. However, I have yet to figure out a way to then run the power query steps to clean it up after it's imported. Currently I am going into the PQ advanced editor and changing the filepath to the new report file.
Is there a way to hard code the M code into VBA and run it on the applicable worksheet? Can I have the end users save the query but have the source dynamically change to the current worksheet after the report has been imported? I would ideally like to avoid having the user save the query as I would then have to help them change the filepath as it will be a different location for every user. Below is my VBA and M code, any help would be greatly appreciated!
VBA Code:
Sub PaidHours()
Dim FileToOpen As Variant
Dim Openbook As Workbook
Dim ShName As String
Dim Sh As Worksheet
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Select Sheet", FileFilter:="Excel Files (*.xls*),*.xls*")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
If FileToOpen <> False Then
Set Openbook = Application.Workbooks.Open(FileToOpen)
ShName = Application.InputBox("Enter the sheet name to copy", "Enter the sheet name to copy")
For Each Sh In Openbook.Worksheets
If UCase(Sh.Name) Like "*" & UCase(ShName) & "*" Then
ShName = Sh.Name
End If
Next Sh
'copy data from the specified sheet to this workbook - updae range as you see fit
Openbook.Sheets(ShName).Range("A1:CF1100").Copy
ThisWorkbook.Worksheets("Paid Hours").Range("A1").PasteSpecial xlPasteValues
Openbook.Close False
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Power Query:
let
Source = Csv.Document(File.Contents("C:\Users\TRD6855\Documents\RTA\Mountain\Accountability Report\Accountability 7-13.txt"),[Delimiter=",", Columns=1, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Split Column by Positions" = Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByPositions({0, 13, 16, 19, 27, 31, 42, 48, 59, 65, 72, 86, 91, 103, 121, 124, 128}), {"Column1.1", "Column1.2", "Column1.3", "Column1.4", "Column1.5", "Column1.6", "Column1.7", "Column1.8", "Column1.9", "Column1.10", "Column1.11", "Column1.12", "Column1.13", "Column1.14", "Column1.15", "Column1.16", "Column1.17"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Positions",{{"Column1.1", type text}, {"Column1.2", type text}, {"Column1.3", type text}, {"Column1.4", type text}, {"Column1.5", type text}, {"Column1.6", type text}, {"Column1.7", type text}, {"Column1.8", type text}, {"Column1.9", type text}, {"Column1.10", type text}, {"Column1.11", type text}, {"Column1.12", type text}, {"Column1.13", type text}, {"Column1.14", type text}, {"Column1.15", type text}, {"Column1.16", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type1", "Actual Time", each [Column1.10]),
#"Trimmed Text" = Table.TransformColumns(#"Added Custom",{{"Actual Time", Text.Trim, type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Trimmed Text",":",".",Replacer.ReplaceText,{"Actual Time"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Replaced Value",{{"Actual Time", type number}}),
#"Added Custom1" = Table.AddColumn(#"Changed Type2", "MECH NUM", each Text.End([Column1.1],7))
in
#"Added Custom1"
Quick note: I am using the same PaidHours sub to import payroll info as well, using "("Text Files (*.txt), *.txt")" as the file filter on a duplicate sub. I know the one I pasted is for excel files only.
Last edited by a moderator: