Index with Match By Code

LearnMeExcel

Well-known Member
Joined
Aug 11, 2009
Messages
746
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
HI
i am trying to learn VBA
and i try this code
Code:
Sub IndexwithMatch()
Dim LR As Integer
LR = Range("a" & Rows.Count).End(xlUp).Row
WorksheetFunction.Index(range("a4:a" & LR),WorksheetFunction.Match(Range("a1"),range("b4:b" & LR),false))
End Sub
with this data
Excel Workbook
AB
1Local
2
3Names
4Name1not
5Name2Yet
6Name3Do
7Name4Local
8Name5ok
Sheet1
Excel 2007

i know we can do it by Formula
Code:
=INDEX(A4:A8,MATCH(A1,B4:B8,0))
but
what is the wronge with my code
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hello

Try.

Code:
Sub IndexwithMatch()
Dim LR As Integer
LR = Range("a" & Rows.Count).End(xlUp).Row
result = WorksheetFunction.Index(Range("a4:a" & LR), WorksheetFunction.Match(Range("a1"), Range("b4:b" & LR), False))
End Sub
 
Upvote 0
You have the correct formula, it's just expecting to return a string from this function, which is not set up, so the compiler doesn't know what you want it to do. Consider:

Code:
Sub IndexwithMatch()
Dim LR As Integer
Dim myStr As String

LR = Range("a" & Rows.Count).End(xlUp).Row

myStr = WorksheetFunction.Index(Range("a4:a" & LR), WorksheetFunction.Match(Range("a1"), Range("b4:b" & LR), False))

MsgBox (myStr)

End Sub
 
Upvote 0
Try.

Code:
Sub IndexwithMatch()
Dim LR As Integer
LR = Range("a" & Rows.Count).End(xlUp).Row
result = WorksheetFunction.Index(Range("a4:a" & LR), WorksheetFunction.Match(Range("a1"), Range("b4:b" & LR), False))
Cells(1, 3) = result
Range("C2") = result
End Sub
 
Upvote 0
Thanx Meldoc
it is working now
that is mean i have to make String name to haold the result when i am working with worksheeetfunction in VBA
 
Upvote 0

Forum statistics

Threads
1,216,562
Messages
6,131,422
Members
449,651
Latest member
Jacobs22

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