EXCEL VBA: VBA CODE for built-in VLOOKUP function - vba

I'm recently learning VBA and making my own UDFs.
So I need some references and built-in functions (like vlookup) must be good examples.
Is there any code open?
Thank you

This should work for you.
Function MyLookupFunc(v As Variant)
MyLookupFunc = _
Application.WorksheetFunction.VLookup(v, ActiveSheet.Range("A1:B100"), 2, False)
End Function
Use in a cell formula like ...
=MyLookupFunc(A1)

Related

UDF is not recognised in excel 2010

I am having a problem with incorporating User Defined Functions in excel. For some reason the function is not recognised. The (simplified) code is as follows:
Option Explicit
Option Base 1
Function Dummy(A As Range, _
B As Range, _
C As Double, _
D As Double) As Double
' This function doesn't do anything
End Function
When I use the function in a cell it returns the #NAME? error.
Strangely enough when I started typing "=du" in the cell excel did find the function.
When I use the error checking function of excel this clearly shows that the function is not recognised.
I know this error can occur in case the VBA module is stored in a user form, a sheet or "ThisWorkbook". However that is not the case here.
Can anyone tell me what I am doing wrong here?
Rename your module or your function. The module name and the function name cannot both be "Dummy".

Howto convert excel formula to work in VBA?

I have found this terrific tip:
http://vlookupweek.wordpress.com/2012/03/27/richard-schollar-vlookup-left
However, the formula
=VLOOKUP(F2,CHOOSE({1,2},$C$2:$C$7,$A$2:$A$7),2,False)
works if entered in a cell but in VBA it gives a syntax error:
Set Rng = Range(Application.WorksheetFunction.Choose({1, 2}, colMax.Address, colId.Address))
Since this works althought it doesn't do what I want:
Set Rng = Range(Application.WorksheetFunction.Choose(1, colMax.Address, colId.Address))
it seems to be the {...} that is the problem. The problem with googling this is that I don't know what {...} is called (array, sequence list...???).
So the question is how to use this tip in VBA.
Just use the Evaluate function to evalute the formula in VBA. There is no need of "translating" it to VBA:
See this link
http://msdn.microsoft.com/en-us/library/office/ff193019(v=office.15).aspx

Excel VBA function works in Visual Basic, but fails in Worksheet

I'm trying to build a 2D array of data using "CurrentRegion".
Function ProcessData()
Dim dataList()
dataList = Range("A1").CurrentRegion
' TODO Process the dataList
End Function
When I test this within Visual Basic (Run/F5), it works great; my dataList is built with no problems. However, if I set a cell in my worksheet to:
= ProcessData()
the function silently fails at the "CurrentRegion" step. Why does this happen? How do I remedy it?
If you call a Function from an Excel cell (i.e. as an User-Defined-Function/UDF), you can only access the ranges that are handed to the function via parameters. Any access to other ranges (and .CurrentRegion is a range) will result in a "Circular Reference" potential cancellation of the execution.
Also, in a UDF you cannot modify anything on the worksheet - but only return the result of function!
For further details, check out this link.
I've just encountered this Q&A having the same problem. I think there is a kind of bug for using CurrentRegion within UDF and the reason is not as Peter suggest in his answer.
Compare these two function:
Function GetAddressOfColumn(TopCell As Range)
GetAddressOfColumn = TopCell.CurrentRegion.Address
End Function
Function GetAddressOfColumnOK(TopCell As Range)
GetAddressOfColumnOK = Range(TopCell, TopCell.End(xlDown)).Address
End Function
Both function use different kind of range references. If Peter were right both should return false result. So, look at the data range below and the result of both function. As you can see second function result is as expected and correct.

Using Excel Formula functions (ERF, ERFC) in Excel VBA code?

I'm trying to use the error function and the complimentary error function in my program. Neither are working. I keep getting the error Compile Error: Sub or Function not defined. However, if I go into a cell and manually try to use the error function, it works.
Why is that? How can I use it in my actual code?
The error function is ERF(x) and the complimentary error function is ERFC(x).
Here's an example of things that don't work:
Sub SeriouslyWHYIsntThisWorking()
x = 3
Range("A1") = Erf(x)
End Sub
Even this doesn't work:
Sub PleaseWork()
Range("A1") = Erfc(1)
End Sub
But if I went into Excel and typed =ERF(3) or =ERFC(1) into a cell, it'll work.
I'm very new to this and probably missing something incredibly simple. Help would be greatly appreciated!
Do you have the Analysis Toolpak for VBA add-in installed/referenced? (Look for atpvbaen.xls)
The ERF function is part of that add-in, and there are two versions of it (one for use in Excel functions, and one for VBA), and you'll need the right one set up and referenced by your project to be usable.
The add-ins are standard from MSFT, but not always set up by default. If you can use it in Excel normally, then you've already set up at least the Excel version. So using it all like it looks like you want to do will require the add-in, regardless of how you implement/use that function. Meaning, if you want to share this with anyone else, they will need the add-in installed/activated.
To link together this answer with the others provided (which are equally accurate and correct), either setting a cell value with
Range("A1").value = Application.WorksheetFunction.ERF(x)
or setting a cell formula with
Range("A1").Formula = "=Erfc(" + x + ")"
will require the end-user using the add-in.
To use a worksheet formula in vba, you need to put Application.WorksheetFunction. in front of it.
Some functions do have vba equivalents, but (as far as I know) not in the case of erf and erfc
Try this:
Sub ThisShouldWorkNow()
x = 3
formula = "=Erfc(" + x + ")"
Range("A1").Formula = formula
End Sub
Totally untested, since I don't have Excel on my Linux machine... But I think I'm getting the point across -- you need to use the .Formula property of the Range object.
There's more information here:
http://msdn.microsoft.com/en-us/library/office/gg192736.aspx

Create a custom worksheet function in Excel VBA

I have a faint memory of being able to use VBA functions to calculate values in Excel, like this (as the cell formula):
=MyCustomFunction(A3)
Can this be done?
EDIT:
This is my VBA function signature:
Public Function MyCustomFunction(str As String) As String
The function sits in the ThisWorkbook module. If I try to use it in the worksheet as shown above, I get the #NAME? error.
Solution (Thanks, codeape): The function is not accessible when it is defined ThisWorkbook module. It must be in a "proper" module, one that has been added manually to the workbook.
Yes it can. You simply define a VBA function in a module. See http://www.vertex42.com/ExcelArticles/user-defined-functions.html for a nice introduction with examples.
Here's a simple example:
Create a new workbook
Switch to VBA view (Alt-F11)
Insert a module: Insert | Module
Module contents:
Option Explicit
Function MyCustomFunction(input)
MyCustomFunction = 42 + input
End Function
Switch back to worksheet (Alt-F11), and enter some values:
A1: 2
A2: =MyCustomFunction(A1)
The word input needs to be replaced as it is a basic keyword. Try num instead. You can also go further by specifying a type, eg variant.
Function MyCustomFunction(num As Variant)
MyCustomFunction = 42 + num
End Function