How to loop through shapes by name and move to various cell references

Jamie McMillan

Board Regular
Joined
Nov 8, 2021
Messages
169
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have been using VBA for years, self taught. But I haven't been using loops, etc. (I can generate some simple loops): but I cannot loop through shapes by there name.
The shapes are named, a, b, c, .... Others are named, 1 , 2, 3, ...

Part of the code
ActiveSheet.Shapes.Range(Array("a")).Left = Cells(Range("v1").Value, Range("w1").Value).Left
ActiveSheet.Shapes.Range(Array("a")).Top = Cells(Range("v1").Value, Range("w1").Value).Top
ActiveSheet.Shapes.Range(Array("b")).Left = Cells(Range("v2").Value, Range("w2").Value).Left
ActiveSheet.Shapes.Range(Array("b")).Top = Cells(Range("v2").Value, Range("w2").Value).Top
ActiveSheet.Shapes.Range(Array("c")).Left = Cells(Range("v3").Value, Range("w3").Value).Left
ActiveSheet.Shapes.Range(Array("c")).Top = Cells(Range("v3").Value, Range("w3").Value).Top
ActiveSheet.Shapes.Range(Array("d")).Left = Cells(Range("v4").Value, Range("w4").Value).Left

I just copy and paste then manually renames the relevant parts.

I need, Array("a"), Range("v1") & Range("w1") set as variables.

I have tried to many things.

Thank you for any help. :)
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Jamie()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("a", "V1", "b", "V2", "c", "V3")
   For i = LBound(Ary) To UBound(Ary) Step 2
      With Cells(Range(Ary(i + 1)).Value, Range(Ary(i + 1)).Offset(, 1).Value)
         ActiveSheet.Shapes(Ary(i)).Left = .Left
         ActiveSheet.Shapes(Ary(i)).Top = .Top
      End With
   Next i
End Sub
 
Upvote 0
Solution
Welcome to the MrExcel board!

Give this a try with a copy of your workbook.
VBA Code:
Sub MoveEm()
  Dim itm As Variant
  Dim i As Long
  
  For Each itm In Split("a|b|c", "|")
    i = i + 1
    With ActiveSheet.Shapes(itm)
      .Left = Cells(Range("V" & i).Value, Range("W" & i).Value).Left
      .Top = Cells(Range("V" & i).Value, Range("W" & i).Value).Top
    End With
  Next itm
End Sub
 
Upvote 0
Sub Jamie() Dim Ary As Variant Dim i As Long Ary = Array("a", "V1", "b", "V2", "c", "V3") For i = LBound(Ary) To UBound(Ary) Step 2 With Cells(Range(Ary(i + 1)).Value, Range(Ary(i + 1)).Offset(, 1).Value) ActiveSheet.Shapes(Ary(i)).Left = .Left ActiveSheet.Shapes(Ary(i)).Top = .Top End With Next i End Sub
Thank you very much Fluff. I was nowhere near. Works exactly. :)
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,588
Members
449,089
Latest member
Motoracer88

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