Pages

Monday, January 26, 2015

LibreOffice - using INDIRECT() for cell referencing

At times I need to summarize values from another sheet in Libre Office, and get an output that will group some values together. In my example I have a small table that could contain from 10 to 1000 rows. The table I want is to be grouped by date, and I want to know how much I have spent on a specific day.

Lets say we have the following table:
This is a simple table showing us some dates, invoice numbers and amounts. From this table I want to know how much I spent/earned on 06.01.2015 and 11.01.2015. I can quite easily get that with the following formula:

CELL F6: -> =SUMIFS(C2:C10;A2:A10;"06.01.2015")
CELL F7: -> =SUMIFS(C2:C10;A2:A10;"11.01.2015")


But later on I add more values to the table, and I don't want to recreate the references in my formulas. So I use the INDIRECT() function to allow the formula reference the number of rows in the table size itself.
To be able to do that I will need to know the number of the first and last row in the table. And in case you need to reference another sheet you can just type in the sheet name in cell F1.
To get the last row in the table I use the COUNTA() function to COUNT the number of rows containing a value. The last parameter in COUNTA() must be set higher than what I expect the number of table rows to be. So in cell E3 I put in

CELL F1: -> Sheet1
CELL F2: -> 1
CELL F3: -> =COUNT(A1:A1000)-1 


The minus 1 on the end is just to compensate for the Total Orders line at the bottom. Adjust this according to your own table(s).

To use the INDIRECT() -function you'll need to concentrate a bit, or at least I do... I will keep Cell F6 and F7 for comparison in this example so we can see the difference between these two approaches.
Now, we'll add the following to cells F9 and F10:


F9 : =SUMIFS(INDIRECT("C"&$F$2&":C"&$F$3);INDIRECT("A"&$F$2&":A"&$F$3);E9)
F10: =SUMIFS(INDIRECT("C"&$F$2&":C"&$F$3);INDIRECT("A"&$F$2&":A"&$F$3);E10)

Now this is my new table with the included calculations:


As we can see; the content of cells F6 and F7 have not changed, but the content of cells F9 and F10 will change according to the numbers of rows in the table. This approach requires that the table data itself is without blank lines. Usually I have what you see in columns E and F in Sheet2 so I can freely add and remove rows in sheet 1. (In that case I also add the reference to "Sheet1" in Cell F1 into the formula."

The INDIRECT() function I'm using here is abbreviated a little bit for better readability. The complete command in cell F9 can also be written like this:

=SUMIFS(INDIRECT(CONCATENATE("C";$F$2;":C";$F$3));INDIRECT(CONCATENATE("A";$F$2;":A";$F$3));E9)

If we break this down we'll se that the first CONCATENATE gives us: "C1:C18" and INDIRECT will reference this area. Starting with Amount and ending with 250 in column C.
The second CONCATENATE gives us: "A1:A18" wich are the search criteria of the SUMIFS function.

If you get into problems; then try to break down the long formula into smaller parts to see what each part is giving you.


No comments:

Post a Comment