Index and Match with VBA

rex759

Well-known Member
Joined
Nov 8, 2004
Messages
608
Office Version
  1. 365
Platform
  1. Windows
My ultimate goal is to read a cell from a closed workbook.


The code below asks the user to select a workbook so I can obtain the location of the file (name doesn’t change) which is placed on a worksheet. Looks something like this,

A2=C:\Documents and Settings\joe\Desktop\Projects\Data.xlsx

I thought I could manipulate the information so I can use index and match but I can’t seem to incorporate it.

This formula to pull out the filename
A7=LEFT(A2,FIND(".",SUBSTITUTE(A2," ","*",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))))-5)

This formula to add the filename with correct syntax
A14="'"&A7&"[data.xlsx]2011'!"

Index and Match
D10=INDEX(A14&$B$2:$B$2000,MATCH(B2,A14&$A$2:$A$2000,0),0)

Code to add file location
Code:
Sub IndividualWkbks()

Application.ScreenUpdating = False
Dim myFileName As Variant

myFileName = Application.GetOpenFilename("Excel Files, *.xlsx")
 If myFileName = False Then
Exit Sub 'user hit cancel
End If

MsgBox (myFileName)

Range("a2") = myFileName

Application.ScreenUpdating = True
End Sub

Anyone have a better way of doing this?

Thank you,
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,094
Latest member
bsb1122

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