macro run in multiple locations but uses the same file in the multiple locations ie the macro needs to select the file in same folder its in

A Nonomus

New Member
Joined
Oct 20, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
how do I add a Macro that will work with multiple CSV files of the same name. but only search the one thats in the same(file) location. as the spreadsheet using the macro

normally you could use .\ to reference the same folder, but this doesnt work.
any help would be great


My spreadsheet Macro will be copied to sits in multiple location i folders with months of the year. ( July, AUG, Sept.)
CSV is DATA.csv that also exists in (july, AUG, Sept.)
so when the macro runs in July folder it will use folder = "C:\July\AutodeskOutputLog.csv" but when I copy to Aug the file will be folder = "C:\AUG\AutodeskOutputLog.csv"

hope thats been explained clearly enough.
hope you have a great day
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi

I think I undestand you. The file with the macro will be callable as ThisWorkbook. To get the directory path of ThisWorkbook you would use ThisWorkbook.Path. It will return the path without the path separator at the end, so you need to concatenate that back on, e.g. ThisWorkbook.Path & "\" or ThisWorkbook.Path & Application.PathSeparator.
 
Upvote 0
hey Jon, thanks for such a speedy response, sorry bit of a noobie so hope this helps. it runs fine if I specify the the folder location (ie"C:\OutputLog.csv")
I want it to look for a file called OutputLog.csv in same location as spreadsheet (Blank.XLSM)

thanks again
KC

"Sub LoadFromFile()
Dim fileName As String, folder As String

folder = "C:\OutputLog.csv"
fileName = ActiveCell.Value
Range("A1").Select
ActiveCell.Offset(0, 0).Range("A1").Select

With ActiveSheet.QueryTables _
.Add(Connection:="TEXT;" & folder & fileName, Destination:=ActiveCell)
.FieldNames = True
.PreserveFormatting = True
.RefreshOnFileOpen = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub"
 
Upvote 0
Works a treat but but for some reason the file doesn't update when opened ( i think it might be related to Data in memory.) tried clearing Folder = and filename= at end of script. But I created a button to assign the macro too.) and that works well too


Thanks Jon (legend) Von der Heyden :)
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,112
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top