Hi JohnSUN , Thanks For your reply and Write Macro Code… In your Code works only Single Calc Document that is Source Data and Filtered Data based On Condition and Results will be placed in Same Calc Document in Two Sperate Sheets using " copyRange(target.CellAddress, Source.RangeAddress) "
Your Code and Source Format Will Apply to Results
Sub Filter_Animation()
Const CRITERIA = “Animation”
Dim oSheets As Variant, oSourceSheet As Variant, oSheetResult1 As Variant, oSheetResult2 As Variant
Dim oCursor As Variant, oDataArray As Variant
Dim aSourceAddress As New com.sun.star.table.CellRangeAddress
Dim aCellResult1 As New com.sun.star.table.CellAddress
Dim aCellResult2 As New com.sun.star.table.CellAddress
Dim i As Long, j As Long
oSheets = ThisComponent.getSheets()
oSourceSheet = oSheets.getByName("Sheet1")
oCursor = oSourceSheet.createCursor()
oCursor.gotoEndOfUsedArea(True)
oDataArray = oSourceSheet.getCellRangeByPosition(4, 0, 4, oCursor.getRangeAddress().EndRow).getDataArray()
aSourceAddress = oSourceSheet.getCellRangeByPosition(0, 0, 4, 0).getRangeAddress()
If oSheets.hasByName("Result1") Then oSheets.removeByName("Result1")
oSheets.insertNewByName("Result1", oSheets.getCount())
oSheetResult1 = oSheets.getByName("Result1")
If oSheets.hasByName("Result2") Then oSheets.removeByName("Result2")
oSheets.insertNewByName("Result2", oSheets.getCount())
oSheetResult2 = oSheets.getByName("Result2")
aCellResult1 = oSheetResult1.getCellByPosition(0, 2).getCellAddress()
aCellResult2 = oSheetResult2.getCellByPosition(0, 0).getCellAddress()
For i = 2 To UBound(oDataArray)
If Trim(oDataArray(i)(0)) = CRITERIA Then
For j = 0 To 4
aCellResult1.Row = J+2
aSourceAddress.StartRow = i
aSourceAddress.EndRow = i
aSourceAddress.StartColumn = j
aSourceAddress.EndColumn = j
oSourceSheet.copyRange(aCellResult1, aSourceAddress)
Next j
aCellResult1.Column = aCellResult1.Column + 1
aSourceAddress.StartColumn = 0
aSourceAddress.EndColumn = 4
oSourceSheet.copyRange(aCellResult2, aSourceAddress)
aCellResult2.Row = aCellResult2.Row + 1
EndIf
Next i
End Sub
and The Sceen Shots of your Code in Same Calc Document Using copyRange
1 ) Based On your Code … I Write The Macro Code For Two Different Calc Documents, that is Source Data in One Calc Document ( Doc1 ) and Destination or Results Will be Placed On Another Document ( Doc2 ) in Two Separate Sheets.
2 ) Based On your Code - I Slightly Modified, WithOut Using “CopyRange(target.CellAddress, SourceRangeAddress)”
Here is the Code - Two Different Calc Documents
Sub Filter_Action_6_TwoDifferentSheets()
Const CRITERIA = “Action” ’ or Use Dim Criteria As String : ’ Criteria = “Action”
Dim oDoc As Object, oSheets As Object, oSourceSheet As Object
Dim oDoc2 As Object, path As String, Args(), oSheets2 As Object, oSheetResult1 As Object, oSheetResult2 As Object
Dim oCursor As Object, DataArray As Variant, i As Long, j As Long
' oDoc1 - 1st Calc Document - Source Data
oDoc = ThisComponent
oSheets = oDoc.Sheets
oSourceSheet = oSheets.getByName("Sheet1")
oCursor = oSourceSheet.createCursor()
oCursor.gotoEndOfUsedArea(True)
DataArray = oSourceSheet.getCellRangeByPosition(4, 0, 4, oCursor.RangeAddress.EndRow).getDataArray()
' Doc2 - 2nd Calc Document - Desination for getting Data Based On Codition
path = ConvertToUrl("E:\Macros - VBA and STARBasic\StarBASIC\WiseOwl Tutorial\DestCalcDoc.ods")
oDoc2 = StarDesktop.loadComponentFromURL(path, "default", 0, Args())
oSheets2 = oDoc2.Sheets
oDestSheet = oSheets2.getByName("Sheet1")
If oSheets2.hasByName("Result1") Then oSheets2.removeByName("Result1")
oSheets2.insertNewByName("Result1", oSheets2.Count)
oSheetResult1 = oSheets2.getByName("Result1")
If oSheets2.hasByName("Result2") Then oSheets2.removeByName("Result2")
oSheets2.insertNewByName("Result2", oSheets2.Count)
oSheetResult2 = oSheets2.getByName("Result2")
' Ouput Results, Which are going to in the Cell A3 (0,2)
oSheetResult1.getCellByPosition(0, 2).getCellAddress() 'A3
oSheetResult2.getCellByPosition(0, 2).getCellAddress() 'A3 ' (0, 0) A1
Dim FilteredData(), PasteRange1 As Variant, PasteRange2 As Variant, oOutput As Variant
For i = 2 To Ubound(DataArray)
IF Trim(DataArray(i)(0)) = CRITERIA Then ' Criteria Then
For j = 0 To 4
FilteredData() = oSourceSheet.getCellRangeByPosition(j,i,j,i).getDataArray()
Output = FilteredData
PasteRange2 = oSheetResult1.getCellByPosition(Column,2+j)
PasteRange2.setDataArray(oOutput)
PasteRange = oSheetResult2.getCellByPosition(j,2+Row)
PasteRange.setDataArray(oOutput)
Next j
Column = Column + 1
Row = Row + 1
End IF
Next i
End Sub
Here is the Macro Code For Same CalcDocument _ WithOut Using copyRange Method
Sub Filter_Action_7_SameSheet()
Const CRITERIA = “Action” ’ or Use Dim Criteria As String : Criteria = “Action”
Dim oDoc As Object, oSheets As Object, oSourceSheet As Object, oSheetResult1 As Object, oSheetResult2 As Object
Dim oCursor As Object, DataArray As Variant, i As Long, j As Long
oDoc = ThisComponent : oSheets = oDoc.Sheets
oSourceSheet = oSheets.getByName("Sheet1")
oCursor = oSourceSheet.createCursor()
oCursor.gotoEndOfUsedArea(True)
DataArray = oSourceSheet.getCellRangeByPosition(4, 0, 4, oCursor.RangeAddress.EndRow).getDataArray()
If oSheets.hasByName("Result1") Then oSheets.removeByName("Result1")
oSheets.insertNewByName("Result1", oSheets.Count)
oSheetResult1 = oSheets.getByName("Result1")
If oSheets.hasByName("Result2") Then oSheets.removeByName("Result2")
oSheets.insertNewByName("Result2", oSheets.Count)
oSheetResult2 = oSheets.getByName("Result2")
Dim oCellResult1 As New com.sun.star.table.CellAddress
Dim oCellResult2 As New com.sun.star.table.CellAddress
' Ouput Result, Which are going to in the Cell A3 (0,2)
oCellResult1 = oSheetResult1.getCellByPosition(0, 2).getCellAddress() 'A3
oCellResult2 = oSheetResult2.getCellByPosition(0, 2).getCellAddress() 'A3 ' (0, 0) A1
Dim FilteredData As Variant, oOutput As Variant, PasteRange1 As Variant, PasteRange2 As Variant
For i = 2 To Ubound(DataArray)
IF Trim(DataArray(i)(0)) = CRITERIA Then
For j = 0 To 4
FilteredData() = oSourceSheet.getCellRangeByPosition(j,i,j,i).getDataArray()
oOutput = FilteredData
PasteRange1 = oSheetResult1.getCellByPosition(Column, j+2)
PasteRange1.setDataArray(oOutput)
PasteRange2 = oSheetResult2.getCellByPosition(j, Row+2)
PasteRange2.setDataArray(oOutput)
Next j
Column = Column + 1
Row = Row + 1
End IF
Next i
End Sub
Here Is the Source File and Screen Shots
Two Different Calc Documents


‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
Same Calc Document


‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
SourceFile
Part 25 - Arrays.ods (22.9 KB)