You are here:

Excel/writing a formula into VBA


Hi Tom

I have written the following formula:

=IF(AG1=AM1,VLOOKUP(A2,$A$S2:$A$T200,2,FALSE) IF(AH1=AM1,VLOOKUP(A2,$A$S2:$A$T200,2,FALSE) IF(AI1=AM1,VLOOKUP(A2,$A$S2:$A$T200,2,FALSE)  IF(AJ1=AM1,VLOOKUP(A2,$A$S2:$A$T200,2,FALSE) IF(AK1=AM1,VLOOKUP(A2,$A$S2:$A$T200,2,FALSE) IF(AL1=AM1,VLOOKUP(A2,$A$S2:$A$T200,2,FALSE)))))),)

Is it possible to write this in VBA?

Christopher Mitchell

==<added usage example at the bottom>===

first, your function isn't really a legitimate function so it leaves the request open to interpretation.  I would think what you are actually asking is this:


Then when  you say VBA, are you looking for a userdefined function written in VBA that you can use in a worksheet.  If so, then I see 4 Ranges that would need to be passed to the function plus the column to return.

r1:  would be the value to find in range r2.  In your example, cell AM
r2:  would be the range of values to find r1.  In your example, cells AG1:AK1
then the look up values
r3:  would be the value to find in the vlookup function. In your example A2
r4:  would be the lookup range.  In your example $AS$2:$AT$200 (I think)
icol:  the column of the value to return in the lookup range .  column 2
I will assume you want an exact match so the function won't look for the 4th argument of Vlookup - the False

this works for me as I designed it. I don't know if it is what you want:

It should be placed in a general module (in the VBE, Insert => Module)

Public Function MyVlookup(r1 As Range, r2 As Range, r3 As Range, r4 As Range, icol As Long)
Dim ans As Variant, v As Variant
Debug.Print r1.Address, r2.Address, r3.Address, r4.Address, icol
ans = Application.Match(r1, r2, 0)
v = Application.VLookup(r3, r4, icol, False)
If IsError(ans) Or IsError(v) Then
    MyVlookup = ""
    MyVlookup = v
End If
End Function

here was how it looked in a cell:

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 All rights reserved.