When I talked about FormulaArray I meant something like this:
Sub copyFormattedFormulas(oSourceRange As Variant,oTargetRange As Variant)
Dim aFormulas As Variant, i As Long, j As Long
oTargetRange.getSpreadsheet().copyRange(oTargetRange.getCellByPosition(0, 0).getCellAddress(), oSourceRange.getRangeAddress())
aFormulas = oTargetRange.getFormulaArray()
For i = LBound(aFormulas) To UBound(aFormulas)
For j = LBound(aFormulas(i)) To UBound(aFormulas(i))
If Left(aFormulas(i)(j),1) <> "=" Then aFormulas(i)(j) = ""
Next j
Next i
oTargetRange.setFormulaArray(aFormulas)
End Sub
I suggest using .clearContents() instead of the second step. Something like this:
Sub copyAny(oSourceRange As Variant,oTargetRange As Variant, Optional aInclude As Variant)
Dim nFlag As Long
Dim sExclude As String
If IsMissing(aInclude) Then aInclude = Array("VALUE","DATETIME","STRING","ANNOTATION","OBJECTS")
oTargetRange.getSpreadsheet().copyRange(oTargetRange.getCellByPosition(0, 0).getCellAddress(), oSourceRange.getRangeAddress())
nFlag = 0
For Each sExclude In aInclude
Select Case UCase(Trim(sExclude))
Case "VALUE"
nFlag = nFlag + com.sun.star.sheet.CellFlags.VALUE
Case "DATETIME"
nFlag = nFlag + com.sun.star.sheet.CellFlags.DATETIME
Case "STRING"
nFlag = nFlag + com.sun.star.sheet.CellFlags.STRING
Case "ANNOTATION"
nFlag = nFlag + com.sun.star.sheet.CellFlags.ANNOTATION
Case "FORMULA"
nFlag = nFlag + com.sun.star.sheet.CellFlags.FORMULA
Case "HARDATTR"
nFlag = nFlag + com.sun.star.sheet.CellFlags.HARDATTR
Case "STYLES"
nFlag = nFlag + com.sun.star.sheet.CellFlags.STYLES
Case "OBJECTS"
nFlag = nFlag + com.sun.star.sheet.CellFlags.OBJECTS
Case "EDITATTR"
nFlag = nFlag + com.sun.star.sheet.CellFlags.EDITATTR
Case "FORMATTED"
nFlag = nFlag + com.sun.star.sheet.CellFlags.FORMATTED
End Select
Next sExclude
oTargetRange.clearContents(nFlag)
End Sub
To test these snippets you can use something like
Sub tests
oSource = ThisComponent.getSheets().getByIndex(0)
oSourceRange = oSource.getCellRangeByName("B3:E26")
oTargetRange = oSource.getCellRangeByName("H11:K34")
copyFormattedFormulas(oSourceRange,oTargetRange)
Print "After call copyFormattedFormulas()"
Rem constants group CellFlags
Rem These constants select different types of cell contents. The values can be combined.
Rem VALUE selects constant numeric values that are not formatted as dates or times.
Rem DATETIME selects constant numeric values that have a date or time number format.
Rem STRING selects constant strings.
Rem ANNOTATION selects cell annotations.
Rem FORMULA selects formulas.
Rem HARDATTR selects all explicit formatting, but not the formatting which is applied implicitly through style sheets.
Rem STYLES selects cell styles.
Rem OBJECTS selects drawing objects.
Rem EDITATTR selects formatting within parts of the cell contents.
Rem FORMATTED selects cells with formatting within the cells or cells with more than one paragraph within the cells
copyAny(oSourceRange,oTargetRange,Array("Value","Strings"))
Print "After call copyAny(Value,Strings)"
copyAny(oSourceRange,oTargetRange)
Print "After call default copyAny()"
End Sub
It’s a bit of a pity that you didn’t show either the spreadsheet with the test data or the code you came up with.