Howto convert excel formula to work in VBA? - 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

Related

Why there is an error when it case using vlookup for int type

I need to use vlookup module in VBA of EXCEL
So I Use the "Application.vlookup"
But There is an error when it case using vlookup for int type
If Use 'vlookup' as a basic function in EXCEL,
There is no error.
But It happend when I use "Application.vlookup" in VBA of EXCEL
Please note capture.
I really want to know how it can be solved
As #Tim Williams has mentioned in the comment, the problem is that your range is C6:D12 and your data is on C4:D10.
In VBA, instad of writing:
Range(Range("C" & 6), Range("D" & 12))
you may simply write Range("C6:D12"), it is 100% the same and is really understandable.
MSDN - refer to Cells and Ranges

VBA Code for Vlookup

When I reference a specific cell in vlookup VBA code like below I can get the code to return the correct answer.
Application.VLookup(Sheets("Setup").Cells(2, 1),
Sheets("Download").Range("A:G"), 7, 0)
However, if I replace the code with a variable (VLDate) then I get an error
Application.VLookup(VLDate, Sheets("Download").Range("A:G"), 7, 0)
I've tried to make Dim VLDate As String but this didn't work too.
Any suggestions?
In your example Sheets("Setup").Cells(2,1) is not a string.
Cells returns a Range.
Dim VLDate As Range
Set VLDate = Sheet("Setup").Cells(2,1)
If your first example worked, using the above code to set the value of VLDate should make your second example work too.
Edit
Might have misunderstood your question a little so while the above is true it might not help!
Could you provide an example value for VLDate, and also the cell formatting type and an example value from the range that its looked up in?
I think I know what the problem is. The vlookup's first input has to be of the same data type of what you're looking for. For example if in the cells you are searching you have only numbers, then try dimensioning VLDate as a number, if you try to dim it as string it won't find a match and will give you the type mismatch error.
You can dim VLDate as variant, which is easier, but do not use quotes if you're looking for a number.
try adding, for debugging purposes, the line
MsgBox(CStr(Application.VLookup(Sheets("Setup").Cells(2, 1), Sheets("Download").Range("A:G"), 7, 0))
and if you het a msgbox with Error 2042 it menas it couldn't find a match, therefore you should be using the wrong data type.
The only situation that you use string is if the destination cells contain something like ="21" in their formulas, because then it is a string.
hope it helps

Using Excel Formula in VBA

Currently I have the following formula working perfectly when entered directly into the excel sheet.
=AVERAGEIFS(A:A,A:A,">="&10,A:A,"<="&11)
However, when I attempt to exectue it in VBA it says there is a formatting error.
Formula = Application.WorksheetFunction.AverageIfs(A:A,A:A,">="&10,A:A,"<="&11)
Is there any way to fix this?
You need to use Range() to define ranges in VBA:
Formula = Application.WorksheetFunction.AverageIfs(Range("A:A"), Range("A:A"), ">=10", Range("A:A"), ">=11")

Is [AWorrkbookName] new or old syntax for VBA?

I'm maintaining a piece of code and came across this in a VBA function:
isIn("Fred", [PeopleList])
Where PeopleList is a workbook named range. I've never noticed that syntax before and just wondered is it something I've missed or is it new? Should I pass in the range into the function or is this okay usage?
This [PeopleList] syntax is shorthand for Application.Evaluate("PeopleList") and it will try to evaluate PeopleList as a formula or named range and return the result as a variant containing an array. This usage is OK and has been available in VBA since Excel version 5.

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