Here are some gotchas when creating buttons for macros in LibreOffice Calc 7.1.8.1 10(Build:1)

Button weirdness after clicking twice

You need to deselect the button at the start of the macro, otherwise :uno.Copy will copy the button instead of the selected cell because it has been focused on. Bug 70883

    ThisComponent.CurrentController.Frame.ContainerWindow.setFocus()

Undo entire macro

Add an undo context around your macro. I have no idea why this is not the default because it rarely makes sense to undo each step of a macro.

    dim undo as object
    undo = ThisComponent.getUndoManager()
    undo.enterUndoContext("SortByNameUndoContext")

    ... body of the macro code

    undo.leaveUndoContext()

Note that if you abort the macro early that your undo will be broken, and you need to restart the program.

Fix your macro recording

Sometimes the paste commands are just commented out in macros, so look for rem dispatcher and remove the rem part to uncomment the line.