If I were solving a similar problem for Calc, I would write it down a little differently:
Sub copyShuffledDeck
Dim oSheets As Variant, oSheet As Variant
Dim oColumns As Variant, oColumnA As Variant, oColumnB As Variant
Dim oFirstEmptyCell As Variant, nEndRow As Long
Dim oCellRangeByPosition As Variant, oDataArray As Variant, oSwapElement As Variant
Dim nextRnd As Long, i As Long
oSheets = ThisComponent.getSheets()
oSheet = oSheets.getByName("Sheet1")
oColumns = oSheet.getColumns()
oColumnA = oColumns.getByIndex(0)
oColumnB = oColumns.getByIndex(1)
oFirstEmptyCell = oColumnB.queryEmptyCells().getByIndex(0)
nEndRow = oFirstEmptyCell.getRangeAddress().StartRow-1
oCellRangeByPosition = oColumnB.getCellRangeByPosition(0, 0, 0, nEndRow)
oDataArray = oCellRangeByPosition.getDataArray()
Randomize
For i = LBound(oDataArray) To UBound(oDataArray)
nextRnd = Int((nEndRow + 1) * Rnd)
oSwapElement = oDataArray(nextRnd)(0)
oDataArray(nextRnd)(0) = oDataArray(i)(0)
oDataArray(i)(0) = oSwapElement
Next i
oColumnA.clearContents(1023)
oColumnA.getCellRangeByPosition(0, 0, 0, nEndRow).setDataArray(oDataArray)
End Sub
Pay attention, I do not use the words Worksheets()
, Range()
or Cells()
in the code - they are alien to Calc, Basic will partially (!) understand these terms if you is specifically pointed out to this “be like a VBA”. But I do not recommend doing this.
CardDeck.ods (12.1 KB)