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
Related
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
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))
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)
Nothing really fancy here, altough I need to split the formula in 2 because it is longer than 255 characters (sources: http://www.dicks-blog.com/archives/2005/01/10/entering-long-array-formulas-in-vba/ http://support.microsoft.com/kb/213181).
f1 = "=SI(1=1,SOMME(SI(mySheet!$R:$R = ""something"",SI(mySheet!$AQ:$AQ = AM$1, NBCAR(Incidents!$AP:$AP)-NBCAR(SUBSTITUE(mySheet!$AP:$AP,$B2,"""")))))/NBCAR($B2)-X_X_X())"
f2 = "SOMME(SI(mySheet!$R:$R = """",SI(mySheet!$AQ:$AQ = AM$1, SI(mySheet!$AM:$AM = $A2,SI(DROITE(mySheet!$AP:$AP,6) = $B2,1))))),"""""
.Range("AM2").FormulaArray = f1
.Range("AM2").Replace "X_X_X()", f2
The formulas are in French, which shouldn't really matter. The formula is correctly returned in the cell AM2, so here is the result:
={SOMME(SI(1=1;SI(mySheet!$R:$R = "something";SI(mySheet!$AQ:$AQ = AM$1; NBCAR(mySheet!$AP:$AP)-NBCAR(SUBSTITUE(mySheet!$AP:$AP;$B2;"")))))/NBCAR($B2)-SOMME(SI(mySheet!$R:$R = "something";SI(mySheet!$AQ:$AQ = AM$1; SI(mySheet!$AM:$AM = $A2;SI(DROITE(mySheet!$AP:$AP;6) = $B2;1)))));"")}
However, I get a #NAME! error. If I look into the formula debug, the error comes from my first function, Excel isn't able to interpret SI() (which is the French for IF()). I tried completely removing that IF() clause but then Excel can't interpret the first function which is now SOMME() (SUM()).
If I just click into the formula bar and press CTRL+Shift+Enter (not changing anything), it returns the correct value.
Why doesn't Excel give me the right number after VBA sets the array formula? Why does it work if I just resubmit the formula in Excel? And why is it always having trouble with the first function? I guess there is some other kind of limitation that I am not aware of.
I found another similar issue while browsing the Web but no solution.
Because Excel has .Formula for formulas in English and .FormulaLocal for formulas in the language of the Excel interface.
You need the local version, but there isn't one for arrays. You have to provide array formulas in English.
You could e.g. set this French formula to .FormulaLocal of a temporary (hidden) range, then read back .Formula from the same range and set that as .FormulaArray on the desired range. But you shouldn't, because your code won't work on any Excel version other than French. The most portable thing is to always use English formulas in your code.
I’m trying to have a quite complicated Sum if function more reliable. The situation is that I’m using that formula (see below) to sum values between two dates from a separate sheet in the workbook.
=SUMIFS(wochen!$F11:$BM11;wochen!$F$8:$BM$8;">="&DATE(YEAR(T$8);1;1);wochen!$F$8:$BM$8;"<="&DATE(YEAR(T$8);MONTH(T$8);DAY(T$8)))
The handicap is that I was hoping to implement some Kind of Lookup function or match function to always get the values from the correct row.
So far the sum_range is static and I can’t make sure its picking the correct line.
I already played around with this kind Address / Match Function
=ADDRESS(MATCH($B13;'F_P&L'!$B$1:$B$267;0);MATCH(T$6;'F_P&L'!$F$6:$CP$6;0);;;"wochen!")
into that Sum_Range of the Sum If Formular but that doesn’t work either as it comes out as Text
I’m happy for any Idea which van be implemented via excel Formulas or VBA
I hope I explained everything clear, if not I'm happy to provide more information's
Many Thanks in Advance
Dennis
Here is a simple example which you can extend to your case. It uses the =INDIRECT() formula.
If you know through some way what the correct rows are, say you know your range of interest is in line 11, ...
Put the following in a cell
A1: = 11 ' You can use VLOOKUP etc. to generate the row number
B1: = "F" & A1 & ":BM"&A1 ' That makes B1 read: F11:BM11
C1: = SUM(INDIRECT(B1)) ' This will make it the equivalent of writing =SUM(F11:BM11)
Writing =function(INDIRECT(*anything*)) where *anything* is a string describing the address of a range is the equivalent of writing =function(** selecting the range **)
Note on Performance: Do note, however, that INDIRECT is a volatile functions so any changes in the workbook with calculations set to automatic will cause it to recalculate. So there might be performance implications in certain cases.