INDIRECT() excel - add to formula - excel

I have need to enter a value that can vary. It is the last row in a column. I add this variable cell to A1. The value is $L$9001
I have the following formula: =VLOOKUP($H4,'week ending 04JUL'!$A$1:$L$9001,3,FALSE)
In A1 I have the value $L$9001. This figure may change hence why I need to add it in and can't use a hardcoded value.
In B1 to test I get the correct output I have: =INDIRECT("A1")
This returns $L$9001 as expected
When I try: =VLOOKUP($H4,'week ending 04JUL'!$A$1:INDIRECT("A1"),3,FALSE)
This returns a #VALUE error.
So I need the value in A1 to replace $L$9001 portion of the formula. Any help is greatly appreciated.

You're almost there, Indirect works slightly different. Try this:
=VLOOKUP($H4,Indirect("'week ending 04JUL'!$A$1:"&A1),3,FALSE)
You put the whole range in the Indirect() field, not just the part you're referring to. Also, note the use of quotes (") starting the Range, wrapping your reference into basically a string that Excel uses.

Related

REF Error when using INDIRECT function to create dynamic row reference

I have a matrix grid in "MasterSheetGrid". I have separate sheets that divide this info into certain dimensions, making it easier to handle for the user.
In order to make the file dynamic, i am trying to use INDIRECT Function within a function, to locate which row of the MasterSheetGrid to look for the information before returning.
The formula works when i specify the row manually, but using INDIRECT i receive a REF error, even though nothing is deleted.
Manual Formula =INDEX(MasterSheetGrid!$5:$5,MATCH((XLOOKUP($J6,$5:$5,6:6)),MasterSheetGrid!6:6,0))
Formula to locate the row
=(MATCH($C6,MasterSheetGrid!$C:$C,0))
Attempt to merge both using INDIRECT by referencing the cell where the above formula is stored, which results in REF
INDEX(MasterSheetGrid!$5:$5,MATCH((XLOOKUP($J6,$5:$5,6:6)),(INDIRECT(J2:J2,0))))
Ideally i would like to not have to use a cell to store the lookup row formula in, but i thought if i could create a correct formula with the cell reference, i could repeat for the formula.
Does anyone know what i am doing wrong?
This is the view of the user. The formula would sit within column K
This is the MasterSheetGrid view
Instead of using INDIRECT which would cause recalculation at any change in the file, I recommend using INDEX instead. You refer to a fixed sheet name, therefore no need to use INDIRECT.
=INDEX(MasterSheetGrid!$5:$5,MATCH((XLOOKUP($J6,$5:$5,6:6)),INDEX(MasterSheetGrid!$1:$50,J2,),0))
Would be the equivalent of what you tried.
Proper use of INDIRECT would be:
=INDEX(MasterSheetGrid!$5:$5,MATCH((XLOOKUP($J6,$5:$5,6:6)),INDIRECT("MasterSheetGrid!"&J2&":"&J2),0))
And it's good practice to take the following into account (thanks David Leal):
If the Sheet name you're referring to contains one or more spaces you need to wrap the name in ' like 'Sheet name'!
To refer a range using INDIRECT you can use the following syntax (as a string delimited, for example delimited by "):
=INDIRECT("J2:J10")
for a cell, this works:
=INDIRECT(J2)
but if you try the same for a range:
=INDIRECT(J2:J10) -> #REF!
you get #REF!
If you are going to refer a Sheet from your Worksheet, then you need in all cases to enter the input argument as string:
=INDIRECT("Sheet1!A1:A10")
=INDIRECT("Sheet1!A1")
=INDIRECT("'Sheet 1'!A2") -> When Sheet Name has spaces use (') delimiter
Notes: By the way you are invoking INDIRECT using the second input argument (a1) which is optional with the value 0. It is not required for getting a referring a range as I showed before.
I suspect this is the issue you are having

Excel - Self reference a cell in conditional formatting

I am trying to create a conditional format formula to colour a cell based on the value of itself, the cell next to it on the right, and whether the cell on the first row in the same column is a weekday or not.
Currently it works correctly as follows:
=AND($A$2=0,$B$2=0,WEEKDAY($A$1)<>1,WEEKDAY($A$1)<>7)
I have a lot of cells I'd like to use this formula in, so I thought about creating a cover-all formula that used the ADDRESS function.
My original idea was to use:
=AND((ADDRESS(ROW(), COLUMN()))=0,$B$2=0,WEEKDAY($A$1)<>1,WEEKDAY($A$1)<>7)
(I've changed just one cell reference here for example, but I'd like to change all 4 if possible).
However, when I try to use ADDRESS(ROW(), COLUMN()) in place of an absolute cell reference, the formula doesn't format the cell anymore.
Is there a way to make this cover-all formula work so that I don't have to go through and change the referenced cell values each time for every cell? Am I missing something about the syntax? Or will this simply not work the way I'd like it to? Thanks in advance!
ADDRESS returns a string that looks like a cell address and "$A$2"<>0.Wrap the ADDRESS in INDIRECT or rewrite your original without absolute rows.
=AND(indirect(ADDRESS(ROW(), COLUMN()))=0, indirect(ADDRESS(ROW(), COLUMN()+1))=0, WEEKDAY($A$1)<>1, WEEKDAY($A$1)<>7)
=AND($A2=0, $B2=0, WEEKDAY($A$1, 2)<6)

Excel change strings in formulas while copying

I have a cell number as a string ("A1") in a formula, and this needs to be changed relatively while copying, like it does automatically to normal cell references. so I don't have to visit 400 cells and change them all by hand.
Any way I can manage this? Thanks for the help.
Formula I am using in 400 cells:
=IF(IDENTIC($M$10;"Entries are correct.");IF(ISNUMBER(FIND("A1";$N$5));"S1";IF(ISNUMBER(FIND("A1";$N$6));"S2";IF(ISNUMBER(FIND("A1";$N$7));"S3";IF(ISNUMBER(FIND("A";$N$8));"S4";IF(ISNUMBER(FINDEN("A1";$N$9));"S5";"")))));"")
I don't want to change every single cell as text in all 400 cells, it should change relatively as normal cell references do.
To get a cell reference as a string in Excel you can use the CELL function:
=CELL("address";A1)
Since the second parameter to the function is an actual cell reference, if you copy and paste this formula it will auto-update accordingly.
Bear in mind this will return a string containing an absolute address so it will return "$A$1", not "A1".
CELL(...) will work.
More generally, you can also build the formula as strings using concatenations. This can be useful when you have a list of stuff and you would like to have formulas that refer to those names. Or in your case it would also work.
Building the formula as string with concats & then search & replace
First I build up as much as I can the formula so that the general structure is already formed (only the reference you'll want to change will not be final):
Using your case as an example:
Then, I wrap that nearly-formed formula into a CONCATENATE(). I then arrange things to have a references that I will point to within that CONCAT() that will auto-increment to the text I actually want to include in my formula. Something like this:
=CONCATENATE("IF(FIND(""",A1,"""$A$4),TRUE, FALSE)")
The ,A1, refers to the actual cell, so that cell's content is what will be included in my formula. You can then increment this all the way to build the proper formula for each cell (the 400 ones in your case). You need the triple """ because you want to actually include A1, B1 etc... are text references, not cells.
Then, all you need to do is selected all those formulas, copy them & make sure to choose past as values, say the line under (to keep you concatenate intact so you can edit it later if needed). Then with that pasted line all selected, you use a find & replace to add an "=" sign to the beginning of formulas. Better be too specific on the find & replace, so find "IF(F" and replace by "=IF(F" or similar. Then magic happens:
You can see that A1, C1, D1 have returned TRUE while the rest are #VALUE! because the find method has failed to find the string.

Excel: Specify sheetname in another cell for countif formula

My intention is to use the indirect formula. Here is the code I want to edit:
=COUNTIF('sheetname'!R:R;"x")
This code gives a #ref! error:
=INDIRECT("COUNTIF('"&D85&"'!R:R;"&B98&")")
Cell D85 contains sheetname and B98 contains "x".
My version of Excel needs ; in formulas instead of ,.
Thanks in advance for your expertise and time.
Try this formula:
=COUNTIF(INDIRECT("'"&D85&"'!R:R");B98)
My read on Indirect says that it simply uses the cell reference contained in the cell you specify in the function:
Indirect( cellContainingReference )
In this case, you don't need to specify the second parameter of Indirect.
So, using the assumptions:
sheetName is in cell D85
cellRange is always R:R
criteria for counting is in cell B98 (which does not need Indirect to work)
Your excel uses ';' rather than ','
Your formula for CountIf would be:
=countif(indirect("'"&D85&"'!R:R"); B98)
With some modification it works, I hope this is a good compromise for you. The modified formula is
=COUNTIF(INDIRECT(E1); F1)
and in this case that E1 should contain something like
''sheetname'!R:R
(See the double apostrophes at the beginning.)
F1 should contain the "X" or other value you want to count.

If/Then Excel Formula

I cannot figure out why this formula isn't working:
=IF(A2="160850",TP,IF(A2="202006",BL,IF(A2="203646",MM,IF(A2="203917",KT,IF(A2="200265",MP,IF(A2="201447",JB,IF(A2="170566",VB"")))))))
Cell A2, A3, and so on, represent commossion codes that are specific to a sales rep.
So, if Cell A2 equals 160850, then I need the cell I place this formula in (C2) to display TP, and so on, based on the formula above.
I have checked and this looks okay to me. However, when I enter this formula in, I get a message that states The formula you entered contains an error.
Can anyone assist?
Thanks so much in advance
You need to put your return values inside quotes, otherwise Excel assumes that TP is a valid name in the workbook (i.e., an address, named range, or variable).
=IF(A2="160850","TP",IF(A2="202006","BL",IF(A2="203646","MM",IF(A2="203917","KT",IF(A2="200265","MP",IF(A2="201447","JB",IF(A2="170566","VB")))))))
There may be additional errors, but this one is apparent.
Wouldn't it be simpler to use a lookup table? List all your commission codes in one column, e.g. Y2:Y10 then list the linked sales rep (initials) in the next column (Z2:Z10) and you can then use this formula copied down the column
=VLOOKUP(A2,Y$2:Z$10,2,0)
The last set of double quotes should be parentheses:
=IF(A2="160850",TP,IF(A2="202006",BL,IF(A2="203646",MM,IF(A2="203917",KT,IF(A2="200265",MP,IF(A2="201447",JB,IF(A2="170566",VB)))))))
You also probably want to take the agent numbers out of quotes or it'll return false because the number entered is numeric and you're looking for a string. The Codes should be in quotes, though e.g. IF(A2=160850,"TP",....

Resources