REM ***** BASIC *****
sub filterandrank
strSourceRange = "b2:b30000"
strTargetCell = "E1"
mains = 1
filterDistinct(mains, strSourceRange, strTargetCell)
END SUB
FUNCTION filterDistinct(mains As String, strSourceRange As String, strTargetCell As String, Optional bContainsHeader As Boolean, Optional bCaseSensitive As Boolean )
REM Uses a Filter to copy distinct rows from the specified Source Range into a new Range that starts from the specified Target Cell.
REM <strSourceRange> : specifies the Range to find distinct rows in, e.g. "A1:B99".
REM <strTargetCell> : specifies the Cell to put the first found distinct row in, e.g. "D1".
REM <bContainsHeader> : OPTIONAL - pass TRUE if the Source Range contains a Header.
REM <bCaseSensitive> : OPTIONAL - pass TRUE if case matters while searching for distinct rows.
Dim oSheet As Object, oSourceRange As Object, oFilter As Object
oSheet = ThisComponent.Sheets.getbyindex(mains)
oSourceRange = oSheet.getCellRangebyName(strSourceRange )
oFilter = oSourceRange.createFilterDescriptor( True )
oFilter.SkipDuplicates = True
Dim oFilterFields(0) as new com.sun.star.sheet.TableFilterField
oFilterFields(0).Operator = com.sun.star.sheet.FilterOperator.NOT_EMPTY
oFilter.setFilterFields(oFilterFields)
srow = oSheet.getCellRangebyName(strTargetCell).CellAddress.row
scolumn = oSheet.getCellRangebyName(strTargetCell).CellAddress.column
frow = srow+12345
scell = oSheet.getCellRangebyName(strTargetCell)
scell.string = ""
oTargetRange = oSheet.getcellrangebyposition(3, srow, 4, 1999)
oTargetRange.fillSeries(com.sun.star.sheet.FillDirection.TO_BOTTOM, com.sun.star.sheet.FillMode.SIMPLE,0,0,0)
oFilter.CopyOutputData = True
oFilter.OutputPosition = oSheet.getCellRangebyName(strTargetCell).CellAddress
If Not IsMissing( bContainsHeader ) Then oFilter.ContainsHeader = bContainsHeader
If Not IsMissing( bCaseSensitive ) Then oFilter.IsCaseSensitive = bCaseSensitive
oSourceRange.filter( oFilter )
for i = 0 to 6
tissel = oSheet.getcellbyposition(3, i)
tissel.value = i
next
End FUNCTION
Last edited by MrProgrammer on Sun Jan 03, 2021 1:58 am, edited 1 time in total.
Reason:Tagged ✓ [Solved]
REM ***** BASIC *****
sub filterandrank
strSourceRange = "b2:b30000"
"
What is the task really?
- Do you want to add numbers by your macro?
- or do you want get the used range dinamically (instead of the permanent range b2:b30000) for the properly working of the macro on the inreased area?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
I want to get numbers dinamically according to number of rows of filtered data. I can make basic write numbers upto last used row. But this code is so long. I thought may be I can get these numbers with a formula similar to this one.
REM ***** BASIC *****
Function LastRowOfUsedArea(lSheetNumber as long, optional oDoc as object) as long
rem The sheet numbers are Zero based values in the macros.
Dim oSheet as object
Dim oDocument as object
Dim oCursor as object
rem Here is a sample for the handling of the "optional passed paramater":
If IsMissing( oDoc ) Then
oDocument = ThisComponent
else oDocument = oDoc
EndIf
rem End of checking of the optional parameter. The function got the passed document or "this" (the actual) one.
oSheet = oDocument.Sheets(lSheetNumber)
oCursor = oSheet.createCursor
oCursor.gotoEndOfUsedArea(False)
LastRowOfUsedArea = oCursor.RangeAddress.EndRow
End function
Function LastColumnOfUsedArea(lSheetNumber as long, optional oDoc as object) as long
rem The sheet numbers are Zero based values in the macros.
Dim oSheet as object
Dim oDocument as object
Dim oCursor as object
rem Here is a sample for the handling of the "optional passed paramater":
If IsMissing( oDoc ) Then
oDocument = ThisComponent
else oDocument = oDoc
EndIf
rem End of checking of the optional parameter. The function got the passed document or "this" (the actual) one.
oSheet = oDocument.Sheets(lSheetNumber)
oCursor = oSheet.createCursor
oCursor.gotoEndOfUsedArea(False)
LastColumnOfUsedArea = oCursor.RangeAddress.EndColumn
End function
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel. The locale for any menus or Calc formulas in my posts is English (USA).