Vba to insert index match formula referencing external workbook

jumbledore

Active Member
Joined
Jan 17, 2014
Messages
262
Hi, I have created a vba code that should insert the index/match formula into a cell and the data for the match function is found in another workbook. However the name of this workbook is saved in another excel file.

I would like it to be pasted as an excel formula. I can easily achieve the desired result using the Worksheet function in vba but the problem with this is that the formula cannot be copied and pasted within the spreadsheet.


Here's the code that I have been working on:

VBA Code:
Sub macro1()

Columns("A:A").EntireColumn.Autofit
Range("B1").Select
ChDir "C:\filename.csv"
Set wb=Selection 'the name of the data file
Set wks = wb.Sheets(1)

Windows("filename.csv" ).Activate
ActiveWindow.Close

Range("B1").Select

' here's where I am getting stuck at:

ActiveCell.FormulaR1C1 = _

"=INDEX('" & wks.name & "'! A:A, MATCH (A1, '" " & wks.name &" '! $K:K, 0),)"

Range("B2").Select

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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