View Single Post
 
Old 01-24-2019, 11:10 AM
MrKim MrKim is offline Windows 10 Office 2019
Novice
 
Join Date: Jan 2019
Posts: 8
MrKim is on a distinguished road
Default Changing Absolute path to Relative in a Macro

Hi all,
I recorded a macro to import a .csv file - the file name will always be named 'x', so x.csv
But as I want the worksheet to be used by others, I went in and attempted to edit the absolute path to the file, to a relative path. The x.csv file will always be located in the same folder as the Excel workbook.

So this is the macro, with the edited path - File.Contents(.\x.csv) -, but it produces an error: Runtime Error 1004
[Expression Error] The name 'Source' wasn't recognized.

What have I done wrong?


Sub Import1()
'
' Import1 Macro
'

'
Application.CutCopyMode = False
ActiveWorkbook.Queries.Add Name:="x", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(.\x.csv),[Delimiter="","", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted " & _
"Headers"",{{""Lot_No"", Int64.Type}, {""Unit"", type text}, {""Cont_UE"", Int64.Type}, {""Int_UE"", Int64.Type}, {""Owners_Name"", type text}, {""Owners_Address"", type text}, {""Owners_Phone"", type text}, {""Owners_Email"", type text}, {""Committee_Member"", type text}, {""Balance"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=x;Extended Properties=""""" _
, Destination:=Range("$A$10")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [x]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "x"
.Refresh BackgroundQuery:=False
End With
ActiveWorkbook.Save
End Sub
Reply With Quote