VBA formula inside cell - vba

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)"

Related

VBA Formula Referencing

I tried recording a formula in excel as a macro =MAX(Q2:Q3) which comes up in the VBA code as a R1C1 reference ActiveCell.FormulaR1C1 = "=MAX(RC[-1]:R[1]C[-1])"
Is there any way I can input the formula in the VBA editor as the Excel reference rather than the R1C1 type of reference?
The Excel MACRO recorder default setting is using the FormulaR1C1, just change to Formula and type the Range you need:
ActiveCell.Formula = "=MAX(Q2:Q3)"
In the future, if you want to use also the Range object, and allow yourself more flexibility, you can use something like the code below:
Dim Rng As Range
Set Rng = Range("Q2:Q3")
ActiveCell.Formula = "=MAX(" & Rng.Address(False, False, xlA1) & ")"
Could you please try to disable the R1C1 reference style from setting. I am not sure which version you are using.
I am using Mac OS, so for me it is something like Excel->Preferences->General->Use R1C1 reference style. Maybe I can check on some windows machine & confirm you, if it is same.
Regards,
Avdhesh

Inserting vlookup via formular1c1 in a Macro

I'm trying to fill a Vlookup into a range of cells using .FormulaR1C1 and get an error 1004. I have almost this exact code elsewhere in my macro and it works fine so I'm not sure what's wrong. It's probably a simple fix and I'm just not seeing it...
Here is the code:
Range("W2").Select
Range(Selection, Selection.End(xlDown)).FormulaR1C1 = "=VLOOKUP(RC[-2],$AA$2:$AC$35,3,TRUE)"
If you are going to use R1C1 then all references must be in that format.
Range(Range("W2"), Range("W2").End(xlDown)).FormulaR1C1 = "=VLOOKUP(RC[-2],R2C27:R35C29,3,TRUE)"
But you should always apply the parentage of the sheet to each range object:
With Worksheets("Sheet1") 'Change to your sheet
.Range(.Range("W2"), .Range("W2").End(xlDown)).FormulaR1C1 = "=VLOOKUP(RC[-2],R2C27:R35C29,3,TRUE)"
End With

Setting cell formula which includes "text constants" in Excel cell by VBA

In actual cell formula I can manually set the following formula.
=IF(MONTH(A15)<7,"FY "&YEAR(A15)-1&"/"&RIGHT(YEAR(A15),2),"FY "&YEAR(A15)&"/"&RIGHT(YEAR(A15)+1,2))
I am trying to set this formula by vba using the following code.
ActiveCell.formulaR1C1 = "=IF(MONTH(A15)<7,"FY "&YEAR(A15)-1&"/"&RIGHT(YEAR(A15),2),"FY "&YEAR(A15)&"/"&RIGHT(YEAR(A15)+1,2))"
The VBA compiler displays a compile error: Expected: end of statement. This appears to have a problem with the exclamation marks.
Does anyone now how to include a text constant in a cell formula set by vba code?
You need to use double quotes within the string like so:
ActiveCell.formulaR1C1 = "=IF(MONTH(A15)<7,""FY ""&YEAR(A15)-1&""/""&RIGHT(YEAR(A15),2),""FY ""&YEAR(A15)&""/""&RIGHT(YEAR(A15)+1,2))"
You only use FormulaR1C1 if you pass a reference in R1C1 format, but you're using A1 format. You can also shorten that formula if you wish:
ActiveCell.Formula = "=""FY ""&YEAR(A15)-(MONTH(A15)<7)&""/""&RIGHT(YEAR(A15)+(MONTH(A15)>=7),2)"

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(...)"