VBA Breaking My Formula - vba

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.

Related

Excel formula within VBA

I have quite a few cells that contain formula, then with VBA the outcome of this formula is the value for a variable, like so:
On sheet in cell AS4:
=SUMPRODUCT(MAX((ROW($AE$4:$AE$997))*($AE$4:$AE$997<>"")))
and then in my VBA:
numRows = ws.Range("AS4").Value
However this is starting to get hard to keep track of which cell is feeding which variable, avoiding overwriting those cells on the sheet by accident, etc.
I need to be able to perform this calculation within VBA if I can, removing the need to have "calculation cells" on my sheet.
I have discovered there is a way to use formula with WorksheetFunction, but only found simple examples of this and cannot adapt it to my situation above.
numRows = WorksheetFunction.SumProduct(MAX((ROW($AE$4:$AE$997))*($AE$4:$AE$997<>"")))
Is not going to work...
Is there a way to do this, or am I better scrapping the idea of using formula and using a pure VBA method?
With help from SJR this was the answer:
numRows = [=SUMPRODUCT(MAX((ROW(Weights!$AE$4:$AE$997))*(Weights!$AE$4:$AE$997<>"""")))]
A bit more research taught me that evaluate(" ") can be just replaced with square brackets [ and ]. Although, if I had variables in the mix of this formula or the formula wasn't constant then I would have to use Evaluate.
I also needed to add the sheet name to the formula as this formula was no longer functioning within the sheet and AE4:AE997 was no longer referring to the correct sheet.
Doubling up on quotes is also necessary as it is code and sees " differently to a formula on the sheet

IF function in 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)

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

How to put a formula into a cell by Excel VBA?

I'm trying to put this =IF(D49>0,D49-D50-D51+D52+D53,) into a cell D54 by using macro.
My code is as follow
Range("D54").FormulaR1C1 = "=IF(D52>0,D49-D50-D51+D52+D53,)"
But when I run the macro.
Cell D54 gets this =IF('D52'>0,'D49'-'D50'-'D51'+'D52'+'D53',) instead.
Excel adds quotation marks to each dells in the formula, render the formula useless. How can I get macro to into formula as formula?
I think you are having issues because you are using the R1C1 formula property for your range, yet you are using classical cell references in your formula. You should be using the Range("D54").Formula property. So your code would look like:
Range("D54").Formula = "=IF(D52>0,D49-D50-D51+D52+D53,)"
You could just use Range("D54").Value = "=IF(D52>0,D49-D50-D51+D52+D53,)"
I have used that method before and hand good results with it.

VB code for VLOOKUP

I'm completely new to VBA. I tried recording a macro of using VLOOKUP function in my spreadsheet, but turns out VLOOKUP recorded like that does not work.
I have a working spreadsheet and I am comparing values in the column "Material" with a different workbook that is like a database for all the materials and their respective programs. The program the material number matches to in the database will be recorded under column "Program" in the working spreadsheet. I use VLOOKUP to do this, but I am having a hard time coming up with a macro for it.
As suggested, I did the following:
ActiveCell.FormulaR1C1 = "'Program"
Range("K41").Select
ActiveCell.Formula = "VLOOKUP(D41,"'C:\Users\Username\Desktop[Database.xlsx]Sheet1!R1C1:R152289C4,4,FALSE)"
But it still does not populate the "Program" column. What am I doing wrong?
The exact line for vlookups can be done in two ways;
1) This does a vlookup within the coding environment, it's more difficult to make it dynamic
application.worksheetfunction.vlookup(worksheets("Material").range("A2"), worksheets("Program").range("A1:F10"),4,0)
2) Make the formula in the cell the vlookup
range("c2").select
activecell.formula = "=vlookup(A1,Material!A1:F10,4,0)"
and then use the environment to fill in.
Maudise
Big thank you to all who took the time to address my question. I took suggestions, played around with the formula, and this is what I got:
=VLOOKUP($D41,'C:\Users\Username\Desktop[Database.xlsx]Sheet 1'!$A$1:$B$40973,2,FALSE)
Does exactly what I wanted it to do! Thanks again!