While I'm a beginner with Macros, a common reference for BASIC macros (which is what yours looks like) is
OpeOffice.org BASIC Guide
While the PDF shows that it's for OpenOffice 3.2, I'm unaware of anything that is specific for the later versions, and unaware of anything that works in 3.2 that doesn't work in the later versions.
I did a quick look to see if I could find something that matches or is similar to your function (that way it might already be built in), but found nothing that, imo, was close enough. But, by all means, look yourself... I may have missed it, or I may have been using the wrong search.
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.