Moving Shapes

P

Preschool Mike

Let me see if I can explain this correctly....I have 10 shapes/picutres on my
worksheet. Shapes 1 - 5 we'll call movable and shapes 6 - 10 unmovable.
What I'd like to happen is that by clicking on one of my movable shapes and
then by clicking on one of my unmovable shapes the movable shape moves on top
of or inside the unmovable shape. This would be a random selection and
placement of shapes (e.g., any movable shape can be moved to any unmovable
shape). Is this possible? I know how to do this in powerpoint but have no
clue with excel.

In powerpoint I would do something like this...
Dim myShape As Shape
'Assign the shape to be moved to this macro
Sub MoveThisShape(theShape As Shape)
Set myShape = theShape
End Sub

'Assign the shape that its moved to to this macro
Sub MoveShapeTo(thePlacementShape As Shape)
myShape.Top = thePlacementShape.Top + 1
myShape.Left = ThePlacementShape.Left + 1
End sub
 
D

Dave Peterson

It's kind of the same in excel, but different <vbg>.

I added 10 shapes to a test worksheet. I named them nicely.

Mov1, Mov2, Mov3, Mov4 and Mov5
and UnMov1, ..., UnMov5

Then I put this code in a General module:

Option Explicit
Sub JustBeep()
Beep
End Sub
Sub DoTheMove()

Dim UnMovShape As Shape
Dim MovShape As Shape
Dim ALittleBit As Double

Dim myRnd As Long
Dim UpperBound As Long
Dim LowerBound As Long

LowerBound = 1
UpperBound = 5

Randomize
myRnd = Int((UpperBound - LowerBound + 1) * Rnd + LowerBound)


Set UnMovShape = ActiveSheet.Shapes(Application.Caller)

Set MovShape = ActiveSheet.Shapes("Mov" & myRnd)

ALittleBit = UnMovShape.Height * 0.2
With MovShape
.Top = UnMovShape.Top + (ALittleBit)
.Left = UnMovShape.Left + (ALittleBit)
.Width = UnMovShape.Width - (ALittleBit * 2)
.Height = UnMovShape.Height - (ALittleBit * 2)
End With

MovShape.ZOrder zordercmd:=msoBringToFront

End Sub

I assigned the "JustBeep" macro to each of the 5 moveable objects. And I
assigned the DoTheMove procedure to each of the unmoveable objects.

With the shape of the objects (I used buttons from the Forms toolbar) that I
used, the 20% reduction of the button size based on height worked nicely. You
may want something different -- based on .width or even an actual number????
 
P

Preschool Mike

Thanks for your help but first a couple of follow up questions.

First, how do I name the shapes? Do I do this in the Name Manage? Second,
will this code allow the person to place the selected shape (one assigned to
Beep) in an unMovable shape of their choice? That's what I'm looking for.

--
Mike Mast
Special Education Preschool Teacher


Dave Peterson said:
It's kind of the same in excel, but different <vbg>.

I added 10 shapes to a test worksheet. I named them nicely.

Mov1, Mov2, Mov3, Mov4 and Mov5
and UnMov1, ..., UnMov5

Then I put this code in a General module:

Option Explicit
Sub JustBeep()
Beep
End Sub
Sub DoTheMove()

Dim UnMovShape As Shape
Dim MovShape As Shape
Dim ALittleBit As Double

Dim myRnd As Long
Dim UpperBound As Long
Dim LowerBound As Long

LowerBound = 1
UpperBound = 5

Randomize
myRnd = Int((UpperBound - LowerBound + 1) * Rnd + LowerBound)


Set UnMovShape = ActiveSheet.Shapes(Application.Caller)

Set MovShape = ActiveSheet.Shapes("Mov" & myRnd)

ALittleBit = UnMovShape.Height * 0.2
With MovShape
.Top = UnMovShape.Top + (ALittleBit)
.Left = UnMovShape.Left + (ALittleBit)
.Width = UnMovShape.Width - (ALittleBit * 2)
.Height = UnMovShape.Height - (ALittleBit * 2)
End With

MovShape.ZOrder zordercmd:=msoBringToFront

End Sub

I assigned the "JustBeep" macro to each of the 5 moveable objects. And I
assigned the DoTheMove procedure to each of the unmoveable objects.

With the shape of the objects (I used buttons from the Forms toolbar) that I
used, the 20% reduction of the button size based on height worked nicely. You
may want something different -- based on .width or even an actual number????
 
D

Dave Peterson

To name it, you can select the shape (I like to rightclick on it), then use the
namebox to the left of the formula bar. Remember to hit enter when you're done
with the name.

But no, this will take a random moveable object and place it over the unmoveable
object that was clicked.

That's what I thought you meant when you wrote that...

I think you're going to have to use two macros. One that indicates what shape
should be used and one that does the real work.

For this code, you don't have to use nice names. You just have to make sure
that you've assigned the macro to the correct shapes.

Option Explicit
Dim MovShape As Shape
Sub JustBeep() 'Should be BeepAndSet(), since it's doing 2 things
Beep
Set MovShape = ActiveSheet.Shapes(Application.Caller)
End Sub
Sub DoTheMove()

Dim UnMovShape As Shape
Dim ALittleBit As Double

If MovShape Is Nothing Then
MsgBox "Please click on one of the moveable shapes first!"
Exit Sub
End If

Set UnMovShape = ActiveSheet.Shapes(Application.Caller)

ALittleBit = UnMovShape.Height * 0.2
With MovShape
.Top = UnMovShape.Top + (ALittleBit)
.Left = UnMovShape.Left + (ALittleBit)
.Width = UnMovShape.Width - (ALittleBit * 2)
.Height = UnMovShape.Height - (ALittleBit * 2)
End With

MovShape.ZOrder zordercmd:=msoBringToFront

'clear out for next time
'or reuse that same shape???
Set MovShape = Nothing 'this clears it.

End Sub


Preschool said:
Thanks for your help but first a couple of follow up questions.

First, how do I name the shapes? Do I do this in the Name Manage? Second,
will this code allow the person to place the selected shape (one assigned to
Beep) in an unMovable shape of their choice? That's what I'm looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top