Inserting VLOOKUP formula in a cell - vba

I am trying to insert a VLOOKUP formula in a cell. I am not even trying to substitute parameters yet. I use this code:
myCell.Offset(0, 4).Value = "=VLOOKUP(A2;AnotherWorksheet!$A$1:$B$231;2;FALSE)"
And I am receiving an error: 1004
If I paste that exact formula in a cell, then the formula is recognized and accepted...
Any hint? thanks!

First, use , and not ; when separating the arguments. Second, you're looking for the .Formula property, not .Value. .FormulaR1C1 may work, but for exactness, just use A1-style formula writing for your VLOOKUP. See below.
Code:
Sub Voom()
Range("A1").FormulaR1C1 = "=VLOOKUP(A2,""AnotherWorksheet!$A$1:$B$231"",2,FALSE)"
End Sub
Result:
It shows #N/A because, of course, I have no data available. :)
For your exact code, just use:
myCell.Offset(0, 4).Formula = "=VLOOKUP(A2,""AnotherWorksheet!$A$1:$B$231"",2,FALSE)"
Hope this helps.

You need to use the Formula property.
For example:
myCell.Offset(0, 4).Formula = "=VLOOKUP(A2,""AnotherWorksheet!$A$1:$B$231"",2,FALSE)"
From MSDN, the Formula property and the FormulaR1C1 property documentation.

Related

How to combine indirect and direct references in VBA?

Issue:
I need to call direct and indirect references. I have a column that shifts to the right every time the program is run. The column is being used to collect the sum of the preceding columns. (I'm aware I can combine the sheet1 within the with, however there are pieces of code between the with and sheet select and this particular question focuses on just the .value section.)
Code 1:
Sheets("Sheet1").Select
With Range("AL2")
.Value = "=SUM(B2:RC[-1])"
End With
B2 is being inserted as a string, instead of as a cell name.
I've also tried:
Sheets("Sheet1").Select
With Range("AL2")
.Value = "=SUM(range("B2") & ":RC[-1])"
End With
Sheets("Sheet1").Select
With Range("AL2")
.Value = "=SUM("& B2 & ":RC[-1])"
End With
This particular set of code does not run correctly and returns:
SYNTAX ERROR
So, the problem in this specific instance is that you are mixing R1C1 and A1 style notation.
Anyway, I'd change your code to set the .Formula property instead of the .Value property, and specifically change it to .FormulaR1C1 if you want to use that notation or leave it as .Formula for A1 notation.
Anyway, your original code:
Sheets("Sheet1").Select
With Range("AL2")
.Value = "=SUM(B2:RC[-1])"
End With
This is setting B2 as a string because Excel is recognizing the RC notation, correctly interpreting that "RC[-1]" is a cell reference, but then not understanding what "B2" means.
So, I'd use something like this:
With Sheets("Sheet1").Range("AL2")
.FormulaR1C1 = "=SUM(R2C2:RC[-1])"
End With
That however would leave $B$2 as an absolute cell reference, and AK2 as a relative reference, which is kind of gross. Depending on your needs you could do this:
With Sheets("Sheet1").Range("AL2")
.FormulaR1C1 = "=SUM(R2C2:R2C37)"
End With
Which would lead to both being absolute references.
If you want something more dynamic, you'll need to use string manipulation on the string you're inputting into the worksheet. For instance, if you have r1 as a range object (with address B2) and r2 as a range object (with address AK2), you could do this:
With Sheets("Sheet1").Range("AL2")
.Formula = "=SUM(" & r1.address ":" & r2.address & ")"
End With
If you have questions, let me know.

VBA formula inside cell

I need to put this formula inside a column subset range
=VLOOKUP(SUBSTITUTE(M3;"#";"");$AG$413:$AK$821;5;FALSE)
I wrote this code:
XML.Range("V3:V411").Formula = "=VLookup(Substitute(M3, ""#"", """"), $AG$413:$AK$821, 5, False)"".Value = .Value"
but doesn't work and I get "Select method of range class failed" error
I recommend using the R1C1 format, more stable in macros.
Also, just use the macro recorder.
Don't pass .Value to Excel, it has no idea what that means. What was the plan with that?
.FormulaR1C1 = "=VLOOKUP(SUBSTITUTE(R[2]C[12],""#"",""""),R413C33:R821C37,5,FALSE)"
Or with .Formula:
.Formula = "=VLOOKUP(SUBSTITUTE(M3,""#"",""""),$AG$413:$AK$821,5,FALSE)"

Replace #N/A error when using R1C1 as Vlookup

I'm using the below code to do a fast vlookup, however I'm struggling when the code returns "#N/A" errors.
I've tried to copy and paste special in the hope that the "N/A" will be treated like regular text (then I can replace with any value I want), but that doesn't seem to work.
My code as follows:
lr1 = Worksheets("Risk Explorer greeks").Cells(Rows.Count, "I").End(xlUp).Row
Range("J2:J" & lr1).FormulaR1C1 = "=VLOOKUP(RC[-1], R1C1:R50000C1, 1, False)"
Would appreciate any help! Any ideas on how I can get rid of these "#N/A" errors using VBA?

Paste text as formula

I am creating a string that is a formula. Like in here (this is a simpler example)
If:
A1 is "Sum"
A2 is "D3"
Then B1 is =Concatenate("=",A1,"(",A2,")")
I want a VBA macro that takes the result of the formula in B1 and paste is as a formula in C1.
I need C1 to be the formula =SUM(D3)
I think it involves the PasteSpecial and evaluate, but I can't figure out how.
I don't want to use the INDIRECT function because I want to be able to fill more cells using than formula and the relative references inside.
with Activesheet
.Range("C1").Formula = .Range("B1").Value
End With
I don't know if you explicitly want a command macro, but this thing seems like a great use of a VBA UDF. If you create the UDF:
Function EvalFormula(f As String) As Variant
EvalFormula = Application.Evaluate(f)
End Function
Then in C1 you can call:
=EvalFormula(B1)
Writing this as a UDF is going to eliminate those unpleasant situations where you forgot to run your macro and now your sheet is all out of whack.
I used ActiveSheet.Cells(5, 3).Formula = ActiveSheet.Cells(5, 3).Value to use the text value as a formula
ActiveSheet.Cells(2, 2).Value = ActiveSheet.Cells(1, 2).Value
You can work the rest from that I'd assume :-)

Add a cell formula in Excel via vba

I’m not an Excel or VBA expert but I want to insert this current excel formula into cell’s using VBA.
Current Excel formula:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
VBA formula :
ActiveSheet.Range("a" & ActiveSheet.Rows.Count).End(xlUp).Offset(2, 12).Value = "=IF(OR(ISNUM(R[-3]C[-9])=FALSE;ISNUM(R[0]C[-9])=FALSE);'';IF(R[0]C[-10]='Total';R[0]C[-9]-R[-3]C[-9];''))"
It doesn’t work… Someone can help me please?
Try using .formula = instead of .value = in your VBA code.
Setting the .value of a cell simply copies in whatever value you specify. In this case, your formula is simply converted to a string value.
Using the .formula property, you are actually specifying the formula that gets used to compute the value, which is what you are looking for.
Can I first suggest a simplification of your formula, from:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
...to...
=IF(AND(C573="Total"; ISNUM(D570); ISNUM(D573)); D573-D570; "")
Then, I'd set a cell (the active cell in the example below) to use that formula using the VBA code:
ActiveCell.Formula = "=IF(...)"