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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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
Sorry yes change the type to variant instead of long for myRow.
 
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,215,455
Messages
6,124,935
Members
449,195
Latest member
Stevenciu

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