Index/Match using VBA not working

rizzo93

Active Member
Joined
Jan 22, 2015
Messages
299
Office Version
  1. 365
I've looked over some posts here related to my issue, but can't seem to get this to work.

I want to use Index/Match using VBA to find a specific cell value in a named range.

VBA Code:
Dim myDistro As String
Dim myID As Integer
myDistro = Application.WorksheetFunction.Index(Worksheets("PS Console - Data").ListObjects("tblPSConsoleRAW").ListColumns("Distribution List"), Application.WorksheetFunction.Match(myID, Worksheets("PS Console - Data").ListObjects("tblPSConsoleRAW").ListColumns("ID"), 0))

The code above does not find the value I'm looking for, but if I change it to this, it will work:
VBA Code:
myDistro = Application.WorksheetFunction.Index(Worksheets("PS Console - Data").Range("AR3:AR47"), Application.WorksheetFunction.Match(myID, Worksheets("PS Console - Data").Range("C3:C47"), 0))

My columns must be dynamic to capture the data as it changes. What am I missing?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Dim myID As Integer
But in your code I don't see what value the myID variable has.

But assuming it has a value, you need to set the Range property
Rich (BB code):
  myDistro = Application.WorksheetFunction.Index(Worksheets("PS Console - Data").ListObjects("tblPSConsoleRAW").ListColumns("Distribution List").Range, Application.WorksheetFunction.Match(myID, Worksheets("PS Console - Data").ListObjects("tblPSConsoleRAW").ListColumns("ID").Range, 0))

I recommend you use the following objects for your sheet and for your table to reduce that line of code and make reading more practical.
Rich (BB code):
Sub index_match()
  Dim myDistro As String
  Dim myID As Integer
 
  Dim sh As Worksheet
  Dim tbl As ListObject
 
  Set sh = Sheets("PS Console - Data")
  Set tbl = sh.ListObjects("tblPSConsoleRAW")
 
  myID = 4    'for example
 
  myDistro = WorksheetFunction.Index(tbl.ListColumns("Distribution List").Range, _
             WorksheetFunction.Match(myID, tbl.ListColumns("ID").Range, 0))
End Sub

;)
 
Last edited:
Upvote 0
Solution
But in your code I don't see what value the myID variable has.

But assuming it has a value, you need to set the Range property
Rich (BB code):
  myDistro = Application.WorksheetFunction.Index(Worksheets("PS Console - Data").ListObjects("tblPSConsoleRAW").ListColumns("Distribution List").Range, Application.WorksheetFunction.Match(myID, Worksheets("PS Console - Data").ListObjects("tblPSConsoleRAW").ListColumns("ID").Range, 0))

I recommend you use the following objects for your sheet and for your table to reduce that line of code and make reading more practical.
Rich (BB code):
Sub index_match()
  Dim myDistro As String
  Dim myID As Integer
 
  Dim sh As Worksheet
  Dim tbl As ListObject
 
  Set sh = Sheets("PS Console - Data")
  Set tbl = sh.ListObjects("tblPSConsoleRAW")
 
  myID = 4    'for example
 
  myDistro = WorksheetFunction.Index(tbl.ListColumns("Distribution List").Range, _
             WorksheetFunction.Match(myID, tbl.ListColumns("ID").Range, 0))
End Sub

;)
That worked, @DanteAmor! Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,988
Members
449,093
Latest member
Mr Hughes

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