Hi there,
I'm new to this forum and i'm in need of some help in some excel coding as I know nothing of it.
I have used record macro for a certain excel sheet to import data from one excel workbook into this particular workbook but this has hard coded the path (C:\Users\shweerasinghe\Desktop\New folder) in the connection sting. The code is as follows:
Sheets("Master Data-CMB Bench").Select
Cells.Select
Selection.Delete Shift:=xlUp
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Excel Files;DBQ=C:\Users\shweerasinghe\Desktop\New folder\CMB Bench Report.xlsx;DefaultDir=C:\Users\shweerasinghe\Desktop\N" _
), Array("ew folder;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;")), _
Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT * FROM `C:\Users\shweerasinghe\Desktop\New folder\CMB Bench Report.xlsx`.`'Master Data$'`" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceConnectionFile = _
"C:\Users\shweerasinghe\Documents\My Data Sources\CMB Bench Report.xlsx 'Master Data$'.odc"
.ListObject.DisplayName = "Table_CMB_Bench_Report.xlsx__Master_Data"
.Refresh BackgroundQuery:=False
End With
When this same workbook is put on another machine, it throws an error because the specified path is wrong. Is there anyway that i could put a dynamic path here so that whoever opens it from their machine will be able to go ahead with the task without having to re-set the path themselves? It can't be done through the wizard as an error is thrown. I also can change the path manually and it will work fine but is there any variable or something that could be put into those specific areas where the path has been hard coded so that the path will be picked up automatically?
Please help...
Thanks in advance =)
Shevi
Bookmarks