Move a shape vba

julhs

Active Member
Joined
Dec 3, 2018
Messages
476
Office Version
  1. 2010
Platform
  1. Windows
I have the below code that cuts and pastes a Group of shapes to a new location.
What is frustrating me is that I can’t find the way to JUST use “Center on Column:AS”, instead of resort to using
Left = Destrng.Left – 35
This is Sub code
VBA Code:
Public Sub MoveShapeToLrwD2()

Dim sht As Worksheet
Dim rng As Range
Dim FrwD As Long
Dim LrwD As Long
Dim Destrng As Range
Dim shp As Shape
    Set sht = ThisWorkbook.ActiveSheet
    Set rng = Range("AK:AK").Find(What:="Paid To", LookIn:=xlValues, LookAt:=xlWhole)
         FrwD = rng.Row
         LrwD = sht.Cells(sht.Rows.Count, "AT").End(xlUp).Row + 3
    Set Destrng = Range("AS" & LrwD)
    Set shp = ActiveSheet.Shapes("Group 1")

    ActiveSheet.Shapes.Range("Group 1").Select
  With shp
    .Left = Destrng.Left - 35
    .Top = Destrng.Top
 End With
   Range("AT" & FrwD).Select  'Effectivly DE-SELECTS Group 1
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try
VBA Code:
With Shp
    .Top = Destrng.Top + (Destrng.Height - .Height) / 2
    .Left = Destrng.Left + (Destrng.Width - .Width) / 2
End With
 
Upvote 0
I did a straight swop of:
VBA Code:
With shp
.Left = Destrng.Left - 35
.Top = Destrng.Top
End With
With your:
VBA Code:
With Shp
.Top = Destrng.Top + (Destrng.Height - .Height) / 2
.Left = Destrng.Left + (Destrng.Width - .Width) / 2
End With
While yours does centralise then Left/Right on Col:AS, BUT it is not placing the first shape (1st of 5) in
VBA Code:
Destrng = Range("AS" & LrwD + 5)
I don’t know how to interpret the returned values when I step into the sub using your version.
When I step in I see,
VBA Code:
.Top = Destrng.Top + (Destrng.Height - .Height) / 2 = 840.6
.Left = Destrng.Left + (Destrng.Width - .Width) / 2 = 2459.4
Using:
VBA Code:
Debug.Print Destrng.Top + (Destrng.Height - .Height) / 2
Debug.Print Destrng.Left + (Destrng.Height - .Height) / 2
It returns
VBA Code:
.Top = Destrng.Top + (Destrng.Height - .Height) / 2 = 680.349215698242
.Left = Destrng.Left + (Destrng.Width - .Width) / 2 = 2429.3791305542
To summerise,
.Top = Destrng.Top + (Destrng.Height - .Height) / 2 - is placing it INCORRECTLY
.Left = Destrng.Left + (Destrng.Width - .Width) / 2 - is placing it CORRECTLY

Any further thoughts?
 
Upvote 0
:confused:
Maybe you need to leave YOUR top? I.e
VBA Code:
        With Shp
              .Top = Destrng.Top
              .Left = Destrng.Left + (Destrng.Width - .Width) / 2
       End With
 
Upvote 0
Solution
Well that did work.
As there has not been any other alternative suggestion to my original or your revised codes, I have to assume there isn’t another way to do it?
Going to use your revised version because that handles any change to column widths in future, whereas my:
VBA Code:
.Left = Destrng.Left - 35
is fixed, so would require manually changing if column widths are changed

Many thanks for your help.
 
Upvote 0
One of the default properties for the shapes (.Placement) says "Move but don't resize with the cells"
So if columns width at the left of the shape be changed then the shape will still be in the correct position.
Viceversa, if Destrng width changes the shape will lose the desired position and you need to execute a macro that re-set it
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top