+ Reply to Thread
Results 1 to 15 of 15

How to get reference from a cell and use it in a formula

  1. #1
    Registered User
    Join Date
    09-04-2014
    Location
    Taipei, Taiwan
    MS-Off Ver
    2013
    Posts
    14

    Lightbulb How to get reference from a cell and use it in a formula

    HI EVERYONE,

    I’m trying to count how many times the number “2” occurred on the cell “A1” across all three sheets “BIN1”, “BIN2”, “BIN3”, and display the result on the sheet “Count”.
    And I want to use this formula on other cells too. So I wrote this formula : =IF(AND('BIN1'!B2="",'BIN2'!B2="",'BIN3'!B2=""),"", IF('BIN1'!B2=2,1,0)+IF('BIN2'!B2=2,1,0)+IF('BIN3'!B2=2,1,0))
    Since the name of the sheets changes a lot ( I want to use this formula to analyze a lot of different sheets), I was wondering if I can type the name of the sheets on a different sheet and just reference it in the formula.

    This is what I tried : I typed the word BIN1 in the cell C1 on the sheet REF, and wrote this formula: =IF(AND('(=REF!C1)'!B3="",'BIN2'!B3=""),"", IF('BIN1'!B3=2,1,0)+IF('BIN2'!B3=2,1,0)), hoping that I would get the value of =IF(AND('BIN1'!B2="",'BIN2'!B2="",'BIN3'!B2=""),"", IF('BIN1'!B2=2,1,0)+IF('BIN2'!B2=2,1,0)+IF('BIN3'!B2=2,1,0)) but It doesn’t seem to work. Does anyone know how I can achieve this ??


    I really appreciate your help!!! Thank you!

    Ps. I have attached a sample file here. Sample.xlsx

  2. #2
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: How to get reference from a cell and use it in a formula

    Hi.

    If you put a list of all your sheet names in a vertical range somewhere in your master sheet, e.g. G1:G3, then your formula is:

    =SUMPRODUCT(COUNTIF(INDIRECT("'"&G1:G3&"'!B2"),2))

    which will count the total number of times that cell B2 contains a value of 2 across all 3 sheets listed in G1:G3.

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: How to get reference from a cell and use it in a formula

    You can also use INDIRECT(address) in your formula, for instance:
    Please Login or Register  to view this content.
    etc.
    Best Regards,

    Kaper

  4. #4
    Registered User
    Join Date
    09-10-2014
    Location
    Matrix
    MS-Off Ver
    2010
    Posts
    70

    Re: How to get reference from a cell and use it in a formula

    Hi,

    Maybe let's try this:
    Please Login or Register  to view this content.
    If i did not understand you well let me know.

  5. #5
    Registered User
    Join Date
    09-04-2014
    Location
    Taipei, Taiwan
    MS-Off Ver
    2013
    Posts
    14

    Re: How to get reference from a cell and use it in a formula

    WOW. You are such a genius!!! I can't believe this can be done with such a simple formula. I guess I have so much more to learn about excel.

    May I ask, how does this formula achieve what I was trying to do??

    Thank you so much!!!!!

  6. #6
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: How to get reference from a cell and use it in a formula

    Quote Originally Posted by ijl0322 View Post
    WOW. You are such a genius!!! I can't believe this can be done with such a simple formula. I guess I have so much more to learn about excel.

    May I ask, how does this formula achieve what I was trying to do??

    Thank you so much!!!!!
    Sorry - not clear whose post you're replying to!

    Regards

  7. #7
    Registered User
    Join Date
    09-04-2014
    Location
    Taipei, Taiwan
    MS-Off Ver
    2013
    Posts
    14

    Re: How to get reference from a cell and use it in a formula

    Hi guys,

    Thank you all for your help!! I really appreciate it!
    Last edited by ijl0322; 09-10-2014 at 05:56 AM.

  8. #8
    Registered User
    Join Date
    09-04-2014
    Location
    Taipei, Taiwan
    MS-Off Ver
    2013
    Posts
    14

    Re: How to get reference from a cell and use it in a formula

    Quote Originally Posted by XOR LX View Post
    Sorry - not clear whose post you're replying to!

    Regards
    Sorry, I was actually replying to your post. When I was replying there was only one post..

    Your formula works magic but I can't understand it's logic at all. Will you please explain how it's done for me??

    Thank you so much!! I really appreciate your help!!
    Last edited by ijl0322; 09-10-2014 at 05:57 AM.

  9. #9
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: How to get reference from a cell and use it in a formula

    Sure.

    If you're not familiar with formula techniques for working across multiple worksheets, you should make note of this construction using INDIRECT, as it offers practically the only means by which we can perform such calculations.

    Substituting in the values from G1:G3 (i.e. your sheet names), it becomes:

    =SUMPRODUCT(COUNTIF(INDIRECT("'"&{"BIN1";"BIN2";"BIN3"}&"'!B2"),2))

    This array of text strings {"BIN1";"BIN2";"BIN3"} representing your sheet names is then concatenated at both ends, at the back with a syntactically-correct construction representing the cell to be queried (i.e. B2 - note the preceding exclamation mark) and at the front with a single apostrophe. Although this latter is not strictly necessary should the names of the relevant worksheets contain no spaces (as in your case), it is nevertheless good practice to form the construction so.

    Hence, after these concatenations, we will have:

    =SUMPRODUCT(COUNTIF(INDIRECT({"'BIN1'!B2";"'BIN2'!B2";"'BIN3'!B2"}),2))

    and, by virtue of the two facts that COUNTIF is able to operate over 3-D ranges (i.e. multiple worksheets) and that SUMPRODUCT is providing the necessary coercion such that INDIRECT will return an array of range references, not just one, this resolves to:

    =SUMPRODUCT({0;1;1})

    the elements of this array - 0, 1 and 1 - corresponding to the number of entries in B2 having a value of 2 in BIN1, BIN2 and BIN3 respectively. And so this finally resolves to 2, as required.

    Hope that helps!

    Regards

  10. #10
    Registered User
    Join Date
    09-04-2014
    Location
    Taipei, Taiwan
    MS-Off Ver
    2013
    Posts
    14

    Re: How to get reference from a cell and use it in a formula

    Quote Originally Posted by XOR LX View Post
    Sure.

    If you're not familiar with formula techniques for working across multiple worksheets, you should make note of this construction using INDIRECT, as it offers practically the only means by which we can perform such calculations.

    Substituting in the values from G1:G3 (i.e. your sheet names), it becomes:

    =SUMPRODUCT(COUNTIF(INDIRECT("'"&{"BIN1";"BIN2";"BIN3"}&"'!B2"),2))

    This array of text strings {"BIN1";"BIN2";"BIN3"} representing your sheet names is then concatenated at both ends, at the back with a syntactically-correct construction representing the cell to be queried (i.e. B2 - note the preceding exclamation mark) and at the front with a single apostrophe. Although this latter is not strictly necessary should the names of the relevant worksheets contain no spaces (as in your case), it is nevertheless good practice to form the construction so.

    Hence, after these concatenations, we will have:

    =SUMPRODUCT(COUNTIF(INDIRECT({"'BIN1'!B2";"'BIN2'!B2";"'BIN3'!B2"}),2))

    and, by virtue of the two facts that COUNTIF is able to operate over 3-D ranges (i.e. multiple worksheets) and that SUMPRODUCT is providing the necessary coercion such that INDIRECT will return an array of range references, not just one, this resolves to:

    =SUMPRODUCT({0;1;1})

    the elements of this array - 0, 1 and 1 - corresponding to the number of entries in B2 having a value of 2 in BIN1, BIN2 and BIN3 respectively. And so this finally resolves to 2, as required.

    Hope that helps!

    Regards
    Hi, thank you so much for your reply!!

    Sorry, one more question. Right now, the cells that have no value across all three sheets appear as 0. If I would like them to appear as blanks, what can I do?? ( I’m trying to differentiate the ones with no value at all and the ones with no twos. )


    Also, I’m trying to use this formula on 6700 other cells, but excel auto-fills it with =SUMPRODUCT(COUNTIF(INDIRECT("'"&G2:G4&"'!B2"),2)) ,

    =SUMPRODUCT(COUNTIF(INDIRECT("'"&G3:G5&"'!B2"),2))…rather than

    =SUMPRODUCT(COUNTIF(INDIRECT("'"&G1:G3&"'!B3"),2)),

    =SUMPRODUCT(COUNTIF(INDIRECT("'"&G1:G3&"'!B4"),2))….. Is there anything I can do about this??

    Sorry that I have so many questions!! I'm really bad at it..

    Thanks so much, I really appreciate it!!
    Last edited by ijl0322; 09-10-2014 at 10:31 PM.

  11. #11
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: How to get reference from a cell and use it in a formula

    Hi.

    Change the formula in your first cell (that which refers to B2) to:

    =IFERROR(1/(1/(SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2)))),"")

    When you copy this down, the reference to B2 will become, successively, B3, B4, etc., whilst the reference to the range G2:G4 will remain fixed.

    Regards

  12. #12
    Registered User
    Join Date
    09-04-2014
    Location
    Taipei, Taiwan
    MS-Off Ver
    2013
    Posts
    14

    Re: How to get reference from a cell and use it in a formula

    Quote Originally Posted by XOR LX View Post
    Hi.

    Change the formula in your first cell (that which refers to B2) to:

    =IFERROR(1/(1/(SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2)))),"")

    When you copy this down, the reference to B2 will become, successively, B3, B4, etc., whilst the reference to the range G2:G4 will remain fixed.

    Regards
    Hi,

    Thank you so much for taking the time to help me!! You have no idea how much you helped me! I could never figure this out by myself in a million years!! If I'm not asking too much, can you please explain this one for me too ??

    I'm sorry that I'm asking so many questions, but I really do appreciate your help!!

    Thank you again!!

  13. #13
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: How to get reference from a cell and use it in a formula

    Not at all. Happy to explain.

    It's pretty much similar, apart from the fact that we now have this part:

    CELL("address",B2)

    which simply returns a text representation of a cell reference. Here, The above will resolve to $B$2.

    Unlike in the previous formula I gave you, however, in which this value was fixed, here it is dynamically dependent upon the row in which the formula is placed.

    Hence, as it is copied down to successive rows, the above will become:

    CELL("address",B3) (=$B$3)
    CELL("address",B4) (=$B$4)
    ...

    etc.

    Hence:

    SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2))

    becomes:

    SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&"$B$2"),2))

    and I explained how this was resolved in my previous post.

    The only other addition was to satisfy your criterion of returning a blank instead of a zero. The standard way to do this would be to use a simple IF statement and repeat the main clause, i.e.:

    =IF(SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2))=0,"",SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2)))

    which is perfectly fine, and if this makes things easier for you to understand I would suggest you use this version instead.

    The set-up I used simply employs a technique to shorten the formula, which takes advantage of the fact that the only number which results in an error when reciprocated with 1 is zero, so that, if the part:

    SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2))

    is equal to zero, then we will have:

    =IFERROR(1/(1/(0)),"")

    which is:

    =IFERROR(1/(#DIV/0!),"")

    i.e.:

    =IFERROR(#DIV/0!,"")

    (1 divided by #DIV/0! is still #DIV/0!)

    which of course resolves to "", i.e. a blank.

    If the main SUMPRODUCT part does not resolve to zero, but e.g. 2, then we will have:

    =IFERROR(1/(1/(2)),"")

    which is:

    =IFERROR(1/(0.5),"")

    i.e.:

    =IFERROR(2,"")

    and clearly 2 is not an error so this returns 2.

    This is the reason for the "double-reciprocation": errors remain errors; non-errors are "re-reciprocated" back to their original values.

    See here for more on this technique:

    http://excelxor.com/2014/08/29/iferr...-from-results/

    Regards

  14. #14
    Registered User
    Join Date
    09-04-2014
    Location
    Taipei, Taiwan
    MS-Off Ver
    2013
    Posts
    14

    Re: How to get reference from a cell and use it in a formula

    Quote Originally Posted by XOR LX View Post
    Not at all. Happy to explain.

    It's pretty much similar, apart from the fact that we now have this part:

    CELL("address",B2)

    which simply returns a text representation of a cell reference. Here, The above will resolve to $B$2.

    Unlike in the previous formula I gave you, however, in which this value was fixed, here it is dynamically dependent upon the row in which the formula is placed.

    Hence, as it is copied down to successive rows, the above will become:

    CELL("address",B3) (=$B$3)
    CELL("address",B4) (=$B$4)
    ...

    etc.

    Hence:

    SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2))

    becomes:

    SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&"$B$2"),2))

    and I explained how this was resolved in my previous post.

    The only other addition was to satisfy your criterion of returning a blank instead of a zero. The standard way to do this would be to use a simple IF statement and repeat the main clause, i.e.:

    =IF(SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2))=0,"",SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2)))

    which is perfectly fine, and if this makes things easier for you to understand I would suggest you use this version instead.

    The set-up I used simply employs a technique to shorten the formula, which takes advantage of the fact that the only number which results in an error when reciprocated with 1 is zero, so that, if the part:

    SUMPRODUCT(COUNTIF(INDIRECT("'"&$G$2:$G$4&"'!"&CELL("address",B2)),2))

    is equal to zero, then we will have:

    =IFERROR(1/(1/(0)),"")

    which is:

    =IFERROR(1/(#DIV/0!),"")

    i.e.:

    =IFERROR(#DIV/0!,"")

    (1 divided by #DIV/0! is still #DIV/0!)

    which of course resolves to "", i.e. a blank.

    If the main SUMPRODUCT part does not resolve to zero, but e.g. 2, then we will have:

    =IFERROR(1/(1/(2)),"")

    which is:

    =IFERROR(1/(0.5),"")

    i.e.:

    =IFERROR(2,"")

    and clearly 2 is not an error so this returns 2.

    This is the reason for the "double-reciprocation": errors remain errors; non-errors are "re-reciprocated" back to their original values.

    See here for more on this technique:

    http://excelxor.com/2014/08/29/iferr...-from-results/

    Regards
    Hi,

    Thank you so much for replying in such detail !!! You really have a wealth of knowledge, and you literally saved my life. I've been working on this for 4 days already and I couldn't figure it out at all... And I needed to turn it in tomorrow afternoon. I was so desperate and was about to start counting the 6700 of them one by one... Thank you again!!

  15. #15
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: How to get reference from a cell and use it in a formula

    You're welcome! Glad I could help.

    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Glitched formula? IF statement does not work because of a formula in a reference cell
    By Tworksheets in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-04-2013, 02:16 PM
  2. copy formula down entire column while changing formula cell reference
    By am_hawk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-22-2013, 11:34 AM
  3. [SOLVED] Replace absolute cell reference with Indirect cell reference in formula
    By Roothy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-11-2013, 04:46 AM
  4. [SOLVED] Copy Cell Reference formula to 3 rows down but increment formula by only 1 row
    By jwnickman in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-23-2013, 01:57 PM
  5. Dragging formula, change sheet reference, but not cell reference
    By Kalilaya0419 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-13-2013, 04:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1