VBA Index-Match Function Doesn't Work Between Workbooks

radbroman

New Member
Joined
Jun 25, 2021
Messages
1
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
  6. 2007
Platform
  1. Windows
I'm trying to write a custom function that will allow me to index-match data from another spreadsheet (DataSource.xlsx). I pull data from this sheet a lot, so I figured it would be easier to just create a function rather than doing the index-match every time. Below is what I have so far:

VBA Code:
Function CNC(CellRef As Range)

Dim arraywkb As Workbook
Set arraywkb = Workbooks.Open("C:\DataSource.xlsx")
Dim arraywks As Worksheet
Set arraywks = Sheets("Sheet1")

Dim targetwkb As Workbook
Set targetwkb = ThisWorkbook
Dim targetwks As Worksheet
Set targetwks = ActiveSheet

CNC = Application.Index(arraywks.Range("A1:L15000"), Application.Match(targetwks.Range(CellRef), arraywks.Range("A1:A15000"), 0), 11)

End Function

This code works perfectly if I use it in the DataSource workbook. However, I need to get it to work in any workbook I'm currently working in (the active sheet). When I try to use it in other workbooks, I get a #VALUE! error (A value used in the formula is of the wrong data type). It's as if it's not activating the DataSource sheet to pull the data. Is there any way to get this code to work correctly in other workbooks?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You haven't referred to the Arraywkb at all which means vba is looking for arraywks in the active workbook , so it can't find it. just add arraywkb. in front of arraywks. so try this:
VBA Code:
CNC = Application.Index(arraywkb.arraywks.Range("A1:L15000"), Application.Match(targetwks.Range(CellRef), arraywkbarraywks.Range("A1:A15000"), 0), 11)
 
Upvote 0

Forum statistics

Threads
1,215,720
Messages
6,126,443
Members
449,314
Latest member
MrSabo83

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