Link to home
Start Free TrialLog in
Avatar of Carbonecz
Carbonecz

asked on

VLOOKUP and folder name from another cell

Hi,

I have this formula:

VLOOKUP(A1,'C:\[file.xlsx]ExportWorksheet'!$A$1:$B$65536,2,FALSE)

Open in new window

I want other users to download the file.xlsx and have it in whatever folder they want. Using VBA I can get a current folder and put it in another cell like this:

Private Sub Workbook_Open()

Dim path As String
path = Application.ActiveWorkbook.path
Worksheets("AnotherSheet").Range("A1") = path & "\"

End Sub

Open in new window

Then I wanted to do something like this:

VLOOKUP(A1, AnotherSheet!$A$1&'[file.xlsx]ExportWorksheet'!$A$1:$B$65536,2,FALSE)

Open in new window

It doesn't work no matter where I put or omit those ' .

Is there a way to do it? If not using a formula then maybe through VBA? I don't know how to search a worksheet and replace part of a string in all cells that are affected.

Thanks!
Avatar of Saqib Husain
Saqib Husain
Flag of Pakistan image

Try

Private Sub Workbook_Open()
    Dim wb As Workbook
    Dim path As String
    For Each wb In Application.Workbooks
        If LCase(wb.Name) = "file.xlsx" Then
            path = wb.path
            Exit For
        End If
    Next wb
    Worksheets("AnotherSheet").Range("A1") = path & "\"
End Sub
With a formula you need to use the INDIRECT function to create the file path and name string. However, INDIRECT does not work when the source file is closed.

Thanks
Rob H
does not work when the source file is closed.
Same applies to my comment. I assume that the file is open which is why you used

path = Application.ActiveWorkbook.path
Avatar of Carbonecz
Carbonecz

ASKER

The file isn't opened. I need to put its full path there. It works without the file being opened when it's like this:

'C:\[file.xlsx]ExportWorksheet'!$A$1:$B$65536

Open in new window


I need to replace C:\ with the path I get from my script and then probably recalculate all affected cells?
ASKER CERTIFIED SOLUTION
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perhaps you are making it too complicated.
The open file is the file with the formulas, linking to file.xlsx.
In that file you search for the path, and that must mean you expect file.xlsx to be in the same folder as the file with the formulas linking to file.xlsx.

If the 2 files are saved in the same folder when created, and both are copied to another folder, then when opening the file with the formulas, the links will point to the folder the file is in now, and not to where it was.

So if both files are in the same folder, you don't have to search and replace anything.
Thanks! Should've thought about that. I recorded FIND and REPLACE macro and edited it to do what I needed.