# Calc Basic - Access filtered data from macro code

Today I stood into another problem while helping in the creating of a Calc App powered by Basic macros.

The thing is, I'm using calc as a database and I have a sheet where I save all the input data as if it was a database table, and here lies the problem: I search row by row "manually" to find the row from where I get the rest of the data saved, but it is painfully slow, so I though using Calc Filters to speed up the search to only filtered data. Making the filters themselves is relatively "easy" but accessing to that filtered data on the macro is getting really tedious.

I have been reading the helping document called CalcAsASimpleDatabase and in page 17 it mentions how to copy the filtered data to another part of the document, but it never mentions how to just keep the Cell Range as a variable from which I can then rescue the data I need to show in another place.

Here is a simplified version of the db-like sheet I'm using as table for storing the data:

And here the filtered data I would like to rescue from it:

That said, I would rescue more than 1 row of data in the real one, but for easiness here is just 1 row.

Thanks in advance for the help, I hope I have been clear, if not please let me know.

==== EDIT ====

First, here's a reinterpretation of the simplified version of what I'm making, which should make it easier to understand what I'm trying to achieve:

The code then read the "Form" Year, Month and Zone and uses functions to fill first the users data and days making a fillable calendar of some sort, which then is filled with data that was already saved, if it existed that is. With the info we have in Data, it would look like this:

As @Mike Kaganski suggested, here's the code that fills the data for each user (the user list is first generated from another sheet using another function) and that I'm pretty sure isn't optimized at all, it uses other functions and the macro should have globals which actually doesn't have, but at least gives a vision of how I'm loading the data right now, fullfilling Mike's suggestion:

Sub fillSavedData(month as string, year as string,zone as string)
dim origin,destiny, southzone as string
dim i,j,k,n,col, row, endrow,int,startrow,startcol, endcol, totrows as integer
'Where we are going to start filling data in the sheet
startcol=2
startrow=7
'Row numbers in Form sheet
i=startrow
do while isLastRegister(0,i,0)=false 'sheet, row, column; return true if the cell has data or false if not
endrow=endrow+1
i=i+1
loop
endrow=endrow+startrow
'we go through the Data sheet
k=startrow
southzone=zone
totrows=nrows(2,0,0)
endcol=daysOnMonth(month,year) 'month ...
edit retag close merge delete

Besides asking for help with a specific problem on your way to the solution you imagined, it would also help to post initial solution that was "painfully slow", to see if someone would come with an advise how to improve that one without using UI filtering (which is simply rows with height=0).

( 2018-10-18 12:02:11 +0100 )edit

Thanks for the fast response. Well my plan was to use sheet filters described on the document linked above, yet I don't know if they differ or are the same as UI Filters, just used them for a fast and easy way to represent what I want to achieve.

The code itself is in Spanish, and depends on other handmade functions, I will try my best to translate and correctly comment it prior posting it.

( 2018-10-18 12:21:17 +0100 )edit

Sort by » oldest newest most voted

If you are decisive about working with custom code there are three things you should know:
-1- If you visually (UI) select a filtered range, the ThisComponent.CurrentSelection will only contain the subranges not filtered away (also invisible then).
-2- If you create a com.sun.star.sheet.SheetCellRange object (by mySheet.GetCellRangeByName() e.g.) it has a .Rows property giving acces to each row intersecting the range. Each row object has a Boolean property .IsFiltered having the Value True for rows "filtered" away and False otherwise.
-3- Rows hidden without using a filter (by direct user action e.g.) are not set to .IsFiltered=True. They also have their original .Height settings, but are set to IsVisisble=Fale.

===Amendment1===
I don't know a comfortable way to just get unfiltered rows.
There is a CellRange method to get the visible parts: theRange.QueryVisibleCells(). If you can assure that there are no rows hidden though not filtered, you may do something like demonstrated in the attached example.
(Attachment updated 2018-10-19 15:22 CEST)

===Amendment2===
See the new demo announced in my recent comment.

Please report if you find a better way.

more

Thanks a lot for the advices! Yeah that aproaches to what I want to achieve, but I guess there is no way to get only visible (IsFiltered=False) rows without checking each row from a selected range isn't it? That's what I'm trying to achieve so I have a CellRange with only 50 rows instead of 5000 rows.

( 2018-10-18 13:25:35 +0100 )edit

Quoting @XanderN: "...from a selected range..."
Here applies what I wrote under -1-: If the selection actually intersects at least one filtered row you don't get a ShetCellRange object as the CurrentSelection, but a SheetCellRanges (Regard the plural!) object. The ranges accessible this way no longer have intersections with filtered rows. You don't need to inspect every single row, but of course, the treatment of an arbitrary number of ranges is somehow more complicated.

( 2018-10-18 17:35:55 +0100 )edit

My bad for not specifying how I select the range, I meant to select the range using Sheet(x).getCellRangeByPosition(a, b, c, d) in basic code, instead of UI selecting as macro recorder does

( 2018-10-19 09:26:00 +0100 )edit

Well, such a range is not selected at all.

( 2018-10-19 13:55:31 +0100 )edit

The data in the sheet will be only touched by macros, so it would be safe to filter as in your admendment example.

I will try filtering the sheet by macro, then using range.QueryVisibleCells() to reduce the search's range, and deleting the filter before the end so I can create new filters for new searchs and post my findings here.

Thanks a lot for the time and dedication put into giving a factible solution to what I was looking for!

( 2018-10-22 13:18:15 +0100 )edit

If you also do the filtering exclusively by macros I would suggest you don't let the filter hide rows.
Set it up instead to write the resulting data to a dedicated range, best in a different sheet.
If you want to work on the data in addition, you get them by myDataArray = targetRange.GetDataArray
See Sub filterAndCopyResults contained in the new demo.

( 2018-10-22 15:43:22 +0100 )edit

I think you probably don't want to hear this, but may I be the first to gently suggest that you consider moving to a database.

In a database you can have indexes to search for things. This makes searching blindingly fast.

While it's true that a hammer can solve almost any problem, if you're trying to turn in a screw, a screw driver is really what you should be using.

Please consider this direction. I think in the long run you'll be much happier.

more