Align and distribute objects to grid in VBA?

How do I align and distribute a random bunch of objects like this:

To this:

Manually I can select the objects row by row, or column by column, and align center relative to object, then distribute evenly.

I'm trying to write a macro to do this in one-click, but am already stuck at the first step, identifying rows or columns of objects by position.

Is there any perhaps established way of doing this in VBA?