Need help with index, match in other workbook (formula via vba)

bieke

New Member
Joined
Dec 18, 2014
Messages
7
Hi,
I want to write a formula in a cell on a worksheet using <acronym title="visual basic for applications">vba</acronym>.
The formula holds an path name for a certain folder.
I can get the data out of the file in the folder, but it seems that excel doesn't find the file (File_Name), since I got a window showing the folder data and I have to click the right file.
How can I solve this?
FolderName is a string and Item_Name is an integer
File_Name is not a variable but the actual name of the file

This is the code I've used:
Worksheets("Test").Cells(1, 1).Formula = "=INDEX('" & FolderName & "\[File_Name " & Item_Name & ".xlsx]Sheet1'!$1:$1048576,MATCH(Sheet1!B5,'" & Folder & "\[File_Name " & Item_Name & ".xlsx]Sheet1'!$A:$A,0),11)"

Thanks for the responses!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
No, that was just a typo :(, I replaced the variable name with another to post it at het forum :(
Hope, you find another solution.

Code:
Worksheets("Test").Cells(1, 1).Formula = "=INDEX('" & FolderName & "\[File_Name " & Item_Name & ".xlsx]Sheet1'!$1:$1048576,MATCH(Sheet1!B5,'" & FolderName & "\[File_Name " & Item_Name & ".xlsx]Sheet1'!$A:$A,0),11)"

Maybe the problem is your use of Folder instead of FolderName in the MATCH part of your formula.
 
Upvote 0
What would the formula be if entered in a cell? And what do the variables FolderName and Item_Name contain?
 
Upvote 0
What would the formula be if entered in a cell? And what do the variables FolderName and Item_Name contain?

In a cell the formula would be:
=INDEX('F:\4_personal_folder\Bieke\Planning files\[File_Name 2014.xlsx]planning'!$1:$1048576,MATCH(Planning!B5,'F:\4_personal_folder\Bieke\Planning files\[File_Name 2014.xlsx]planning'!$A:$A,0),11)

In vba:
Worksheets("Test").Cells(1, 1).Formula = "=INDEX('" & FolderName & "\[File_Name " & Item_Name & ".xlsx]planning'!$1:$1048576,MATCH(Planning!B5,'" & FolderName & "\[File_Name " & Item_Name & ".xlsx]planning'!$A:$A,0),11)"

FolderName is a string and would contain: F:\4_personal_folder\Bieke\Planning files
Item_Name is an integer and in this case it would contain 2014

If I write it manually in the cell, it does work, but if I write it in the cell using vba, then it failed

Has it something to do with the ' ?
 
Upvote 0
With those values assigned to the variables the cell would contain exactly the formula you posted. Does this return the formula you would expect in the Immediate window?

Code:
Debug.Print "=INDEX('" & FolderName & "\[File_Name " & Item_Name & ".xlsx]planning'!$1:$1048576,MATCH(Planning!B5,'" & FolderName & "\[File_Name " & Item_Name & ".xlsx]planning'!$A:$A,0),11)"
 
Upvote 0
With those values assigned to the variables the cell would contain exactly the formula you posted. Does this return the formula you would expect in the Immediate window?

Code:
Debug.Print "=INDEX('" & FolderName & "\[File_Name " & Item_Name & ".xlsx]planning'!$1:$1048576,MATCH(Planning!B5,'" & FolderName & "\[File_Name " & Item_Name & ".xlsx]planning'!$A:$A,0),11)"

No, I also get the window named "update values File_Name 2014", but, I've found the problem :$
The file was not in the folder I thought is was :$
Sorry.
But thanks for the help!!!
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,772
Members
449,095
Latest member
m_smith_solihull

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