Get the x/y coordinates oft the center of shapes

User avatar
Stefan_Sand
4StarLounger
Posts: 415
Joined: 29 Mar 2010, 11:50
Location: Vienna, Austria

Get the x/y coordinates oft the center of shapes

Post by Stefan_Sand »

Hello,

i have a nice hex grid shapefile of the United Kingdom.
Is there a way to get the x/y coordinates of all centers of the shapes?

Please see the example file, there are 12 Shape(groups) in it.

kind regards,
Stefan
You do not have the required permissions to view the files attached to this post.

snb
4StarLounger
Posts: 587
Joined: 14 Nov 2012, 16:06

Re: Get the x/y coordinates oft the center of shapes

Post by snb »

Code: Select all

Sub M_snb()
   For Each it In Sheet1.Shapes
       MsgBox "y=: " & it.Top + it.Height / 2 & vbLf & "x=: " & it.Left + it.Width / 2, , it.Name
   Next
End Sub

User avatar
Stefan_Sand
4StarLounger
Posts: 415
Joined: 29 Mar 2010, 11:50
Location: Vienna, Austria

Re: Get the x/y coordinates oft the center of shapes

Post by Stefan_Sand »

thanks, but how shall i go through it, if the variables are not declared?

snb
4StarLounger
Posts: 587
Joined: 14 Nov 2012, 16:06

Re: Get the x/y coordinates oft the center of shapes

Post by snb »

Remove 'Option Explicit'

User avatar
HansV
Administrator
Posts: 78591
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get the x/y coordinates oft the center of shapes

Post by HansV »

Or declare

Dim it As Shape

and replace Sheet1 with the codename of the sheet, or with Active sheet.
Best wishes,
Hans

User avatar
SpeakEasy
4StarLounger
Posts: 563
Joined: 27 Jun 2021, 10:46

Re: Get the x/y coordinates oft the center of shapes

Post by SpeakEasy »

>Remove 'Option Explicit'
Not sure I'd ever advise that

User avatar
HansV
Administrator
Posts: 78591
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get the x/y coordinates oft the center of shapes

Post by HansV »

snb NEVER declares any local variable...
Best wishes,
Hans

snb
4StarLounger
Posts: 587
Joined: 14 Nov 2012, 16:06

Re: Get the x/y coordinates oft the center of shapes

Post by snb »

Snb only declares private & public variables and tries to avoid those as much as possible. (see www.snb-vba.eu)
Declaring Local variables is 100% redundant.
So the use of 'NEVER' and 'any' is incorrect and should be replaced by 'Only if necessary'.

User avatar
HansV
Administrator
Posts: 78591
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get the x/y coordinates oft the center of shapes

Post by HansV »

I have edited my previous reply to "any local variable". I strongly disagree with your statement "Declaring Local variables is 100% redundant."
Best wishes,
Hans

User avatar
Stefan_Sand
4StarLounger
Posts: 415
Joined: 29 Mar 2010, 11:50
Location: Vienna, Austria

Re: Get the x/y coordinates oft the center of shapes

Post by Stefan_Sand »

sorry, that i started a debate over declaring variables, i only wanted to get the x / y of shape coordinates into columns C and D and i know the diferences, but as i am not very good in using VBA i declare as often as i could.

thanks anyway.

User avatar
HansV
Administrator
Posts: 78591
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get the x/y coordinates oft the center of shapes

Post by HansV »

See the attached workbook.

GB Tiles.xlsm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Stefan_Sand
4StarLounger
Posts: 415
Joined: 29 Mar 2010, 11:50
Location: Vienna, Austria

Re: Get the x/y coordinates oft the center of shapes

Post by Stefan_Sand »

Thank You, Hans!