Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get my formula to always reference to the last sheet?

I currently have 2 worksheets in my excel file.

The first sheet is known as the Summary page, which displays an summary result of the second sheet.

The second sheet is known as the raw data. An example would be a column named Fruits.

Apple
Apple
Apple
Banana
Banana
Pear

In the first sheet, I would have a formula that counts the number of time the respective fruits appear and the result will be displayed in different cells.

=COUNTIF(Fruits!A2:A7,"Apple")
=COUNTIF(Fruits!A2:A7,"Banana")

What I wanna do is, is it possible for me to program the formula such that everytime I add a new sheet of raw data (3rd sheet), the statistics on the first sheet is able to reference to the latest sheet to get the information.

(Assuming that the positioning of the data and all are the same as the second sheet.)

What I have done so far is to come out with a function GETLASTWSNAME() which is able to always retrieve the name of the last worksheet. but it seems kinda impossible for me to nest the function within the countif formula itself.

=COUNTIF((GETLASTWSNAME())!A2:A7,"Apple)

The above formula is how i want my formula to work, but sadly excel does not allow me to do that.

Any comments would be appreciated. Thanks!

like image 750
Thomas Avatar asked Apr 29 '12 09:04

Thomas


People also ask

How do you reference the last sheet in Excel?

Re: Reference cell in previous sheet Alternatively, type '=' in the cell where you want the linked value, then click on the previoussheet tab and select the cell with the required value and hit enter.

How do you reference a previous sheet in an equation?

To reference a cell or range of cells in another worksheet in the same workbook, put the worksheet name followed by an exclamation mark (!) before the cell address. For example, to refer to cell A1 in Sheet2, you type Sheet2!

How do you keep a formula reference constant in Excel?

If you want to maintain the original cell reference when you copy it, you "lock" it by putting a dollar sign ($) before the cell and column references. For example, when you copy the formula =$A$2+$B$2 from C2 to D2, the formula stays exactly the same. This is an absolute reference.


2 Answers

You can use the XLM/Range Name workaround for this and not VBA if you prefer

  1. Define a range name, wshNames to hold the array of sheet names
    =RIGHT(GET.WORKBOOK(1),LEN(GET.WORKBOOK(1))-FIND("]",GET.WORKBOOK(1)))
    Uses David Hager's technique
  2. Use this Excel formula to extract the last sheet name from the array of sheet names
    =INDEX(wshNames,COUNTA(wshNames)+RAND()*0)

This formula says look at all the sheets, then return the last (using the COUNTA). The RAND()*0) portion ensures that the formula is volatile and updates when Excel does

If you do use VBA you will need to ensure your GETLASTWSNAME function is volatile, i.e. it gets updated when changes occur.

enter image description here

like image 187
brettdj Avatar answered Oct 10 '22 11:10

brettdj


=COUNTIF(INDIRECT(GETLASTWSNAME() & "!A2:A7"),"Apple")

like image 22
Tomalak Avatar answered Oct 10 '22 09:10

Tomalak