IF function in VBA - vba

I want the create the following:
If a cell in column C contains the value TRUE, the corresponding value in column B should be copied to the corresponding cell in column AJ.
This is what I have so far:
Range("AJ2").Select
ActiveCell.FormulaR1C1 = "=IF(C[-33]=TRUE,B:B,0)"
Range("AJ2").Select
Selection.AutoFill Destination:=Range("AJ2:AJ1324")
The problem occurs when I run the macro. The following statement is copied in the cells in column AJ:
=IF(C:C=TRUE;B:(B);0),
where I expect the following:
=IF(C:C=TRUE;B:B;0)
Could someone, please, tell me where my mistake is?
Thanks

ActiveCell.FormulaR1C1 = "=IF(C[-33]=TRUE,RC2,0)"

You are mixing R1C1 and A1 type references in the formula. B:B is not an R1C1 reference.
If you want:
=IF(C:C=TRUE;B:B;0)
Then you need to use:
"=IF(C[-33]=TRUE,C[-34],0)"
I suspect that may not be the formula you really want, but you have not addressed that issue.
One method of obtaining the correct formula is to enter what you really want into the cell, then examine the cell.FormulaR1C1 property to see how that formula is formed in the different notation.
I suspect, from your description, that what you really want would be created by:
"=IF(RC[-33]=TRUE,RC[-34],0)"
which would show, in the cell, as:
=IF(C2=TRUE;B2;0)
or
"=IF(RC3=TRUE,RC2,0)"
producing
=IF($C2=TRUE;$B2;0)

Related

activecell.formula with RC notation and vlookup not working vba

I have this code and I cannot get it to work with RC notation
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[1],ARTICULOS!$A:$I,4,FALSE)"
The value that I'm looking for is 1 column after the "activecell".
As #Scott pointed out, you cannot mix R1C1 and A1 notation.
Whatever it is you're trying to do, there is probably a more efficient way to accomplish it, but for now, this should give roughly your desired result: a VLOOKUP formula with a lookup_value of whatever is in one cell to the right of the ActiveCell.
ActiveCell.Formula = "=VLOOKUP(INDIRECT(ADDRESS(ROW(),COLUMN()+1)),ARTICULOS!$A:$D,4,FALSE)"
Note that I also changed the lookup_range to stop on column D because if you're looking in the 4th column starting with A, then D would be the last needed column.
Also note that I removed R1C1 since it's not needed here.
There's also probably a better method that using INDIRECT(ADDRESS(ROW(),COLUMN()+1)) but I can't think of it just now, and this way works too.
More Information:
Office Support : INDIRECT function (Excel)
Office Support : ADDRESS function (Excel)
BetterSolutions : A1 or R1C1 Notation
Office Support : Create or change a cell reference

Vlookup with three comments depends on found value vba code or formula ( very hard to find solution)

I have very difficult scenario where i need to mention three comments by tow V lookup. let me explain you situation.
We are getting three sheets in workbook with values every weekly.
sheet1 range A1 to A5 value , lookup values sheet2 range A:B and lookup value sheet3 range A:B. yes i can able to put two V lookup , but problem is 3 comments i need to show in single output.
When i lookup values found in sheet2 need comment as "found value in sheet2" , if not it has to lookup sheet3 range if found then it has to give comment as "found value in sheet3" and if value not found need to give comment as " invalid data".
through vba can is it possible and three comments should be sheet1 in a single output based on values?
am looked in google i can use IFSA formula and apply formula but i cant provide three comments ...
For us its really hard every weekend , Please help me with VBA Code or Any formula at one shot for three comments ?
Really appreciate your help
You can actually have up to 7 Nested (IF) functions in Excel - Nested Formula Limit
You need to make sure that you properly close each function. This can be seen when editing the formula, it will highlight the opening/closing parentheses as you arrow through the formula.
If it tries to select a range while using the arrow keys, hit F2 to toggle between Edit/Enter modes.
I think you want this...
=IF(ISNA(VLOOKUP(A1,Sheet1!A1:B4,2,FALSE)),IF(ISNA(VLOOKUP(A1,Sheet2!A1:B4,2,FALSE)),IF(ISNA(VLOOKUP(A1,Sheet3!A1:B4,2,FALSE)),"Invalid Data", "Sheet 3"),"Sheet2"),"Sheet1")
Actually, on second thought, I assume you want whatever is in Column B
=IFNA(VLOOKUP(A1,Sheet1!A1:B4,2,FALSE),IFNA(VLOOKUP(A1,Sheet2!A1:B4,2,FALSE),IFNA(VLOOKUP(A1,Sheet3!A1:B4,2,FALSE),"Invalid Data")))
If I understand correctly, you need to determine if the first VLOOKUP is found, and if not, use the second VLOOKUP. Thats super simple. Assuming the formula Profex provided doesn't work:
=IF(IFERROR(VLOOKUP("A1", Sheet2!A1:B4, 2, False), "Err")="Err",IFERROR(VLOOKUP("A1", Sheet3!A1:B4, 2, False), "Not Found"), VLOOKUP("A1", Sheet2!A1:B4, 2, False))

percentile formula error in VBA

I'm trying to apply a formula through VBA for a particular range. This is the code in my VBA editor:
Sheets("WBR45").Range("AE105").Formula = "=PERCENTILE.INC(TP!$A$3:$A$30:$B$3:$B$30:$C$3:$C$30:$E$3:$E$30,50%)*24"
And the below formula gets updated in the destination cell when this is run:
=PERCENTILE.INC(TP!$A$3:$A$30:$B$3:$B$30:$C$3:$C$30:$E$3:$E$30,50%)*24
But I get an error in the destination cell as #VALUE!.
And when I click on "Show Calculation steps", only this part of the formula is underlined :
TP!$A$3:$A$30:$B$3:$B$30
I have no idea what is wrong with this simple formula. Can someone please take a look
Honestly I have no clue about what you're doing with this, but this may fix it:
"=PERCENTILE.INC(TP!$A$3:$A$30:TP!$B$3:$B$30:TP!$C$3:$C$30:TP!$E$3:$E$30,50%)*24"
You appear to have three errors in your formula:
You are using : to separate ranges instead of ,
You are not specifying which sheet the second, third and fourth ranges refer to, therefore it is defaulting to the sheet on which the formula occurs (i.e. sheet "WBR45")
Multiple ranges will need to be enclosed within brackets (...) in order to be passed as a single range.
If you are trying to have your function operate on the range A3:C30 together with the range E3:E30 (i.e. A3:E30 but ignoring column D), with those ranges being on the "TP" worksheet, I believe that you need to change your formula to
Sheets("WBR45").Range("AE105").Formula = "=PERCENTILE.INC((TP!$A$3:$A$30,TP!$B$3:$B$30,TP!$C$3:$C$30,TP!$E$3:$E$30),50%)*24"
or, slightly simplified
Sheets("WBR45").Range("AE105").Formula = "=PERCENTILE.INC((TP!$A$3:$C$30,TP!$E$3:$E$30),50%)*24"

Excel VBA formula as text issue

In my Excel 2016 VBA I have this code:
Range("M2").Select
ActiveCell.FormulaR1C1 = "=IFERROR(IF(E2=""Sgl"",K2,L2),"")"
when it gets placed in the cell it is the formula not the result. To manually fix this I have to change from Text to General and also the actual code needs changing:
Cell M2 says: =IFERROR(IF(E2="Sgl",K2,L2),")
It should say: =IFERROR(IF(E2="Sgl",K2,L2),"")
with the extra " at the end before the closing parenthesis
This was recorded (I removed the RC to the absolute cell reference initially when it wouldn't work), so I'm not sure what caused this or how to resolve it.
Any help would be appreciated.
Thanks
I think you just need to add the extra quote at the end. Because the quote denotes a string, you need to make two of them to yield one -- which means you need four to yield two.
Range("M2").Formula = "=IFERROR(IF(E2=""Sgl"",K2,L2),"""")"
Other notes of interest:
You don't need to Range().Select and then work against ActiveCell. Just invoke the methods/properties directly on the Range Object
I think in your case Formula will work. R1C1 is handy when the formulas are relative, but in this case, you are referencing actual cells. There is nothing wrong with what you did, but FYI
To generate the two double quotes ("") in IFERROR, you need to put 4 double quotes ("""") in the vba string literal.
I fixed this by removing the IFERROR part as if there was no value in the proceeding columns it simply left it blank anyway rather than showing an error message:
Columns("M:P").Select
Selection.NumberFormat = "General"
Range("M2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-8]=""SGL"",RC[-2],RC[-1])"
Range("M2").Select

VBA Breaking My Formula

I am using an Add-in called SEOTools for Excel which is free : http://seotools.nielsbosma.se/releases/
VBA is breaking my formula.
=Dump(GoogleAnalytics(""ga:1169833"",""ga:adCost,ga:impressions,ga:adClicks,ga:CTR,ga:CPC,ga:goal7Completions"",A307,A306,""ga:campaign"","""",""ga:medium==cpc;ga:campaign!=(not set);ga:campaign!=ZZZ_Old_Mexico"","""",1,10000,FALSE,FALSE))
Whats happening is the A307 shows up as 'A307' when the formula is placed into a cell A1 in Excel which does not work for this formula. Why? Can I stop it adding those characters to the cell reference?
Full code :
Worksheets("Latest Time Range").Select
Range("A1").Select
ActiveCell.FormulaR1C1 = "=Dump(GoogleAnalytics(""ga:1169833"",""ga:adCost,ga:impressions,ga:adClicks,ga:CTR,ga:CPC,ga:goal7Completions"",A307,A306,""ga:campaign"","""",""ga:medium==cpc;ga:campaign!=(not set);ga:campaign!=ZZZ_Old_Mexico"","""",1,10000,FALSE,FALSE))"
Your solution relates to your improper use of FormulaR1C1. Rather than using this method, you should just use .Formula to insert your formula into the cell.