# Revision history [back]

### 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.

### 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 as String, year as Integer; get's a month name and a year and return an Integer with the number of days it has

for i=1 to nrows(3,0,0)+1
if zone="South" then 'North is always North, but South can be South I or South II
'search PID in user sheet and write the exact zone
'k form counter, i for registers, n for personal data
for k=startrow to endrow
if obtainValue(3,i,0)=obtainValue(0,k,0) then
for n=0 to totrows+1
if obtainValue(0,k,0)=obtainValue(2,n,2) then
southzone=obtainValue(2,n,4)
n=totrows
k=endrow
end if
next
end if
k=k+1
next
end if
if mes=obtainValue(3,i,6) and year=obtainValue(3,i,5) and southzone=obtainValue(3,i,2) then
for j=startrow to endrow
if obtainValue(3,i,0)=obtainValue(0,j,0) then
row=j
end if
next

if int=obtainValue(3,i,7) then
col=startcol+int-1
end if
writeString("Form",row,col,0,0,obtainValue(3,i,4))
writeString("Form",row+1,col,0,0,obtainValue(3,i,3))
end if
next
end sub


Here is the .ods of the simplified example I created, as code highlight here doesn't exist