This sub creates a powerquery with the source from a folderpath, it gets all the excel files from the folder and consolidate and filter it.
I am having an error whenever I make Source = Folder.Files("Dir") dynamic
Is there a way to make the paths dynamic so that I will just refresh the query page and update the value of the SourcePath for Folder.Files in my Settings sheet.
I have this error
Runtime-error '1004':
[Expression.Error] The name 'Source' wasn't recognized, Make sure it's spelled correctly.
in this line .Refresh BackgroundQuery:=False
If I create a value for SourcePath referencing a cell in a sheet with this format "C:\Path" ,It has this error
Error:
Runtime Error '1004':
Application Defined or Object-defined error
in this line
.ListObject.DisplayName = "Data"
Sub AddSaveQuery()
'Set Objects
Dim SourcePath As String
Dim SavePath As String
SourcePath = Worksheets("Home").Range("G21").Value
SavePath = Worksheets("Home").Range("G8").Value
Sub AddSaveQuery()
'Set Objects
Dim SourcePath As String
Dim SavePath As String
SourcePath = Worksheets("Home").Range("G21").Value
SavePath = Worksheets("Home").Range("G8").Value
'Will set first condition for sheet2
'Filter Column2 Begins with FilterValue1
ActiveWorkbook.Queries.Add Name:="Data", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""" & SourcePath & """)," & Chr(13) & "" & Chr(10) & " #""Filtered Hidden Files1"" = Table.SelectRows(Source, Each [Attributes]?[Hidden]? <> true)," & Chr(13) & "" & Chr(10) & " #""Invoke Custom Function1"" = Table.AddColumn(#""Filtered Hidden Files1"", ""Transform File"", Each #""Transform File""([Content]))," & Chr(13) & "" & Chr(10) & " #""Renamed Columns1""" & _
" = Table.RenameColumns(#""Invoke Custom Function1"", {""Name"", ""Source.Name""})," & Chr(13) & "" & Chr(10) & " #""Removed Other Columns1"" = Table.SelectColumns(#""Renamed Columns1"", {""Source.Name"", ""Transform File""})," & Chr(13) & "" & Chr(10) & " #""Expanded Table Column1"" = Table.ExpandTableColumn(#""Removed Other Columns1"", ""Transform File"", Table.ColumnNames(#""Transform File""(#""Sample File"")))," & Chr(13) & "" & _
"" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Expanded Table Column1"",{{""Source.Name"", Type text}, {""Column1"", Type any}, {""Column2"", Type any})," & Chr(13) & "" & Chr(10) & " #""Removed Other Columns"" = Table.SelectColumns(#""Column1" & _
"ed Type"",{""Column2""})," & Chr(13) & "" & Chr(10) & " #""Filtered Rows"" = Table.Sel" & _
"ectRows(#""Removed Other Columns"", Each Text.StartsWith([Column2], ""FilterValue1""))" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Filtered Rows"""
ActiveWorkbook.Queries.Add Name:="Parameter1", Formula:= _
"#""Sample File"" meta [IsParameterQuery=true, BinaryIdentifier=#""Sample File"", Type=""Binary"", IsParameterQueryRequired=true]"
ActiveWorkbook.Queries.Add Name:="Transform Sample File", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(Parameter1, null, true)," & Chr(13) & "" & Chr(10) & " Page1_Sheet = Source{[Item=""Page1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Page1_Sheet, [PromoteAllScalars=true])" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Promoted Headers"""
ActiveWorkbook.Queries.Add Name:="Sample File", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""" & SourcePath & """)," & Chr(13) & "" & Chr(10) & " Navigation1 = Source{0}[Content]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Navigation1"
ActiveWorkbook.Queries.Add Name:="Transform File", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = (Parameter1) => let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(Parameter1, null, true)," & Chr(13) & "" & Chr(10) & " Page1_Sheet = Source{[Item=""Page1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Page1_Sheet, [PromoteAllScalars=true])" & Chr(13) & "" & Chr(10) & " in" & Chr(13) & "" & Chr(10) & " #""Promoted Headers""" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Data;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Data]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Data"
.Refresh BackgroundQuery:=False
End With
Workbooks("CognosDataCleaner2.xlsm").Connections.Add2 "Query - Parameter1", _
"Connection To the 'Parameter1' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Parameter1;Extended Properties=""""" _
, "SELECT * FROM [Parameter1]", 2
Workbooks("CognosDataCleaner2.xlsm").Connections.Add2 _
"Query - Transform Sample File", _
"Connection To the 'Transform Sample File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform Sample File"";Extended Properties=""""" _
, "SELECT * FROM [Transform Sample File]", 2
Workbooks("CognosDataCleaner2.xlsm").Connections.Add2 "Query - Sample File", _
"Connection To the 'Sample File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File"";Extended Properties=""""" _
, "SELECT * FROM [Sample File]", 2
Workbooks("CognosDataCleaner2.xlsm").Connections.Add2 "Query - Transform File" _
, "Connection To the 'Transform File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform File"";Extended Properties=""""" _
, "SELECT * FROM [Transform File]", 2
'Rename New Query Sheet to TEST1
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "TEST1"
Sheets("TEST1").Select
Workbooks("Book1").Connections.Add2 "Query - Transform File", _
"Connection To the 'Transform File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform File"";Extended Properties=""""" _
, "SELECT * FROM [Transform File]", 2
Workbooks("Book1").Connections.Add2 "Query - Sample File", _
"Connection To the 'Sample File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File"";Extended Properties=""""" _
, "SELECT * FROM [Sample File]", 2
Sheets("TEST1").Copy
'SAVE PATH
ChDir SavePath
ActiveWorkbook.SaveAs Filename:= _
SavePath _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
I have created an Object SourcePath and in that path I concat it so it does have the value like this:
"Directory"
A1 = "
A2 = "
A3 = Directory or path
=CONCAT(A1 & A3 & A2)
I also have set Source as String to make it an object aswell.
You need quotes round the path, so:
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""" & SourcePath & """),"
The last line is causing some trouble.
MsgBox ("Book Code = " & ISBN & vbNewLine & _
"Book Title= " & title & vbNewLine & _
"Unit Price= " & title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & (Format(CustomerInput * UnitCost), Currency))
Move the close-parenthesis after UnitCost in the Format statement to the end, and Currency in the format statement needs to be a string (i.e: in quotes):
MsgBox ("Book Code = " & ISBN & vbNewLine & _
"Book Title= " & Title & vbNewLine & _
"Unit Price= " & Title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & (Format(CustomerInput * UnitCost, "Currency")))
Although the above works, you can omit some of your parentheses, and possibly make the code a bit easier to understand:
MsgBox "Book Code = " & ISBN & vbNewLine & _
"Book Title= " & Title & vbNewLine & _
"Unit Price= " & Title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & Format(CustomerInput * UnitCost, "Currency")
I am sending mail linked to Excel data. i need to bold and red only few words and i am trying and i am not able to do it. Hope someone from here can help me out. Cells(i,13) has to get bold and Red when i am sending.
toList = Cells(i, 4) 'gets the recipient from col D
eSubject = "Request for KBR transport to " & Cells(i, 2) & " on " & Format(Cells(i, 3), "dd-mmm-yy")
eBody = "Dear " & Cells(i, 1) & vbCrLf & vbCrLf & "Can you please arrange KBR Transport for below PASSENGERS." & vbNewLine & vbNewLine & _
" " & Cells(i, 13) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
"Full Name : " & Cells(i, 6) & vbNewLine & vbNewLine & _
"Nationality : " & Cells(i, 7) & vbNewLine & vbNewLine & _
"Departure/Arrival Date : " & "" & Format(Cells(i, 8), "dd-mmm-yy") & vbNewLine & vbNewLine & _
"Airline : " & Cells(i, 9) & vbNewLine & vbNewLine & _
"Flight Number : " & Cells(i, 10) & vbNewLine & vbNewLine & _
"Departure/Arrival Time : " & Cells(i, 11) & vbNewLine & vbNewLine & _
"Escort required: " & Cells(i, 14) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
" Contact Number: " & Cells(i, 12) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
"Please confirm pick up time and availability." & vbNewLine & vbNewLine ##
Use HTML body instead, then you can use the HTML <b> and <font> tags. It may suit better as you can use a HTML <table> for the data displayed also.
Can someone help me out here with formatting date when I am sending email? In Cell 8 I have date but when I'm sending email date will show as 10/03/2016 but I wanted to show date as 03-Oct-2016. I tried putting code as & Format(Date, "dd/mmm/yy") but when I run the program it will show today's date next to 10/03/2016.
Please see below my coding:
## "Full Name : " & Cells(i, 6) & vbNewLine & vbNewLine & _
"Nationality : " & Cells(i, 7) & vbNewLine & vbNewLine & _
"Departure/Arrival Date : " & Cells(i, 8) & vbNewLine & vbNewLine & _
"Airline : " & Cells(i, 9) & vbNewLine & vbNewLine & _
"Flight Number : " & Cells(i, 10) & vbNewLine & vbNewLine & _
"Departure/Arrival Time : " & Cells(i, 11) & vbNewLine & vbNewLine & _
"Escort required: " & Cells(i, 14) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
" Contact Number: " & Cells(i, 12) & vbNewLine & vbNewLine & _ ##
Change
"Departure/Arrival Date : " & Cells(i, 8) & vbNewLine & vbNewLine & _
to
"Departure/Arrival Date : " & Format(Cells(i, 8), "dd-mmm-yyyy") & vbNewLine & vbNewLine & _
The cells which the code is pointing (b2, b3 ..) are already showing up 2 decimal houses, but the msg box shows many . How can I fix it? I had an attempt with & _
Format(dTotalArea, "0.00"), but didnt work.
Many thanks in advance.
MsgBox "First Response Time (hours)= " & _
Worksheets("Results").Range("B2").Value & _
vbCrLf & "Investigation Time (hours)= " & _
Worksheets("Results").Range("B3").Value & _
vbCrLf & "Nr of Incidents Resolved= " & _
Worksheets("Results").Range("B4").Value & _
vbCrLf & "Resolved Same Day (%)= " & _
Worksheets("Results").Range("B5").Value & _
Format(dTotalArea, "0.00"), , "title_of_msgbox"
Try this
MsgBox "First Response Time (hours)= " & _
Worksheets("Sheet1").Range("B2").Value & _
vbCrLf & "Investigation Time (hours)= " & _
Format(Worksheets("Sheet1").Range("B3").Value, "0.00") & _
vbCrLf & "Nr of Incidents Resolved= " & _
Format(Worksheets("Sheet1").Range("B4").Value, "0.00") & _
vbCrLf & "Resolved Same Day (%)= " & _
Format(Worksheets("Sheet1").Range("B5").Value, "0.00") _
, , "title_of_msgbox"