Solved

VLOOKUP and folder name from another cell

Posted on 2014-04-15
7
1,150 Views
Last Modified: 2014-04-18
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!
0
Comment
Question by:Carbonecz
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40003377
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
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 40003396
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
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40003409
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
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Carbonecz
ID: 40003415
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?
0
 
LVL 33

Accepted Solution

by:
Rob Henson earned 500 total points
ID: 40003674
How about doing a Find and Replace?

Have your formula setup with a spurious filename (eg TempFile.xlsx) then you can do a Find on "C:\Path\TempFile.xlsx" and Replace with File Name generated/found in existing routine.

Do a Find and Replace manually and use the VB Recorder to get the syntax. When recorded, the script will show "Find:=Text" and "Replace:=Text", the two blocks of Text can be replaced with a Variable generated within the script.

Thanks
Rob H
0
 
LVL 21

Expert Comment

by:Ejgil Hedegaard
ID: 40005098
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.
0
 

Author Comment

by:Carbonecz
ID: 40008330
Thanks! Should've thought about that. I recorded FIND and REPLACE macro and edited it to do what I needed.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question