# Search for a sequence of numbers from bottom of spreadsheet...

This question may seem odd and is perhaps downright whimsical. I have searched for macros that could serve my purpose but have not found anything that would search in reverse numerical sequence from the bottom of a spreadsheet. I am using LibraOffice version 4 on Centos 6.5. My spreadsheet has 25 columns and many rows. The cells of each row/column are populated with a number from 1 to 50 and arranged in no particular order. A new row of numbers are frequently added at the bottom of the sheet. I need to write a macro that will:

- Find the last row containing data.
- For each column and value in a cell in the last row, search the previous rows for the next lower value until the entire sequence of numbers (1 through 50 and a max one number decremented per row) have been found. For example, if a cells value on the first column in the last row = 5 then search backwards row by row for the numbers 4,3,2,1,50,49.48...10,9,8,7 and finally 6. Perform this on each column of each row.
- Keep track of the number of rows passed during the search and divide Nrows by 50 for the average rows a number decrements.
- Rerun macro for each new row that is added.

I would really like to read some suggestions or example code on how this can be done Seems that it should be a for loop in reverse?

The task is not clear. If you do not have exact 50 rows, then you will have gaps or double numbers. What to do? Are all rows filled, or do empty cells exist? Why you need to add the new rows at the end? Add it at the beginning and you can use normal search functions. What do you want to do with the resulting average? Why use a macro and not a separate sheet for calculation?

I think you're quickly approaching the threshold where it's just simpler to use bash + grep + awk (or python if you prefer).