Convert Excel formula to VBA code

SalimJ

New Member
Joined
Jul 8, 2013
Messages
22
Hi

Please can you help me convert the following formula to vba code. Moreover, I need to access the Employee table that is found in another workbook.
[Excel formula]
=INDEX(Employee,MATCH("ZJ",Employee[ID],0),COLUMN(Employee[Name]))
[/Excel formula]

 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Something like this?

Code:
Dim ws As Worksheet, myRow As Long
Dim rngID As Range, rngName As Range

Set ws = Workbooks("Book2").Sheets("Sheet1")
Set rngID = ws.Range("Employee[ID]")
Set rngName = ws.Range("Employee[Name]")

myRow = Application.Match("ZJ", rngID, 0)
If Not IsError(myRow) Then MsgBox Application.Index(rngName, myRow)

It will need adjusting to suit your workbook. That will provide the answer in a message box.
 
Upvote 0
Hi Steve the fish,

Thank you very much for the valuable code. However, how do I handle when there is no matching? Actually, I'm getting "Type mismatch".

Please help.

Sorry I'm not used to VBA programming.

Thanks
 
Upvote 0
Hi,

Can anyone please tell me what is wrong with the following. I'm getting "Type Mismatch" on myRow.

Code:
Dim ws As Worksheet, myRow As Variant
Dim rngItem As Range, rngSize As Range
Dim rngRate As Range


Set ws = Workbooks("Book2").Sheets("Sheet1")
Set rngItem = ws.Range("Table[Item]")
Set rngSize = ws.Range("Table[Size]")
Set rngRate = ws.Range("Table[Rate]")


myRow = Application.Match("Jacket" & "Large", rngItem & rngSize, 0)
'myRow = Application.Match(1, ("Jacket" = rngItem) * ("Large" = rngSize), 0)
If Not IsError(myRow) Then MsgBox Application.Index(rngRate, myRow)


Thanks
 
Upvote 0
Try it like this and see if its ok:

Code:
myRow = Evaluate("MATCH(1,(" & rngItem.Address(External:=True) & "=""Jacket"")*(" & rngSize.Address(External:=True) & "=""Large""),0)")
 
Upvote 0
Hi,

It works perfect. Thank you.

Is there a way to return more than one column as result; for example if I want to return [Rate] and [Location]

Thanks
 
Upvote 0
Hi steve the fish,

I got what I wanted regarding the multiple result.

Thank you very much for your help.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,219
Members
453,024
Latest member
Wingit77

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