Macro to copy date from cell above gives problem sorting

I have a macro that processes a row and if the date cell is blank I want to copy the date from the cell above it. The code works fine, but then if I sort that column, the copied cells appear at the bottom of the list, evidently because they are not actually date format. I’ve tried a lot of solutions (chatgpt gave failing examples 9 times!) so how can it be done? here’s a section of what I have now:

oRange = oSheet.getCellRangeByName("A3:Z" & lastRow)
For i = 0 To lastRow - 3
  oCell = oRange.getCellByPosition(0, i) 'date column
  If oCell.getString() = "" Then
    ' Replace empty with value from above
     oCellAbove = oRange.getCellByPosition(0, i - 1)
     oCell.setString(oCellAbove.getString())
     If oCell.Type = com.sun.star.table.CellContentType.TEXT Then
        'Convert text to date
        On Error Resume Next
        oCell.String = CDate(Trim(oCell.String)) ' Converts date formats
        On Error GoTo 0
    End If
  End If
Next i

If you set the string of a cell, it gets a string actually. Your code does not convert text to date. This is AI bullshit.