Relative file paths for Excel Data Connections

According to further research it's not possible without writing a VBA/macro script using the workbook path.


It is possible via VBA, just record a macro while creating the data import and then check the VBA generated. you can modify the macro and use it for loading data from any location.


Create a macro enabled excel workbook(.xlsm), save it in a location and add the following macro:

Sub Auto_Open()
    ' Clean current connections and query tables
    ' https://stackoverflow.com/a/49135238/213871
    Dim cn
    Dim qt As QueryTable
    Dim ws As Worksheet
    For Each cn In ThisWorkbook.Connections
        cn.Delete
    Next
    For Each ws In ThisWorkbook.Worksheets
        For Each qt In ws.QueryTables
            qt.Delete
        Next
    Next ws

    ' Clear all contents except header row
    Rows("2:" & Rows.Count).ClearContents

    ' Add the connection to the csv file in the same folder
    ' Inspired from https://stackoverflow.com/a/40536980/213871

    Dim csvFileName As String
    csvFileName = "DatabaseView.csv"

    Dim filePath As String
    filePath = ActiveWorkbook.Path

    Dim conString As String
    conString = "TEXT;" & filePath & "\" & csvFileName

    ' Add the connection to DataBaseView.csv
    With ActiveSheet.QueryTables.Add(Connection:= _
        conString _
        , Destination:=Range("$A$2"))
        .Name = "DatabaseView"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 1
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

Tweak csv file, range (currently it's "$A$2") and delimiters options (you can record a macro while you add import from UI to get a template). Auto_Open() macro will cause it to load it at file startup. Tested with Excel 2010.