VBA Formula Referencing - vba

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

Related

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

Excel IF cell reference formula being changed when recorded in VBA

I have a macro that works fine. I'm adding a formula to it that describes the status of the workbook for someone else to review. When I record the macro of implementing the formula into the sheet, VBA records it using relative references even though I do not have the relative references button selected. The relative references do not point correctly so I need to fix it. I checked this post (adding a dynamic cell reference in vba) and am now thinking that I should adjust the formula with some VBA reference code but I'm not sure if what the post is using is suitable for me. Am I going in the right direction?
Excel Formula:
=IF(Selections!K2="","Not prepped","Prepped")
When recorded into VBA:
ActiveCell.FormulaR1C1 = _
"=IF(Selections!R[-41]C[-1]="""",""Not prepped"",""Prepped"")"
What I need in VBA code:
ActiveCell.FormulaR1C1 = _
=IF(Selections!K2="""",""Not prepped"",""Prepped"")
ok, then is this your answer:
dim rng as range
set rng = thisworkbook.sheets("Sheet1").range("L5")
rng.Formula = "=IF(Selections!K2="""",""Not prepped"",""Prepped"")"

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

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

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