Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 542
Default Position shapes to be centered in a cell

Hi, Ive got a question about how to re-position a shape (type 13).

Ive got a spreadsheet that has a bunch of little pictures nested each in its
own cell. what im trying to do is center the picture in the center of the
cell. Im getting an error on "selection.shaperange.incrementtop 10" what am i
doing wrong?

any help would be great, thanks in advance

For Each C In Activesheet.Range("E11:BF100")
C.Select
On Error Resume Next 'if no shape in that cell
Selection.ShapeRange.IncrementTop 10
Selection.ShapeRange.IncrementLeft -1
On Error GoTo 0
Next C
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Position shapes to be centered in a cell


Maybe you have Excel 2007 and it allows you to select shapes on a sheet
in this way (Selection.ShapeRange (ie. a range of cells on a sheet
doesn't have its own range of shapes)). Not in Excel 2003.
A sheet has its collection of shapes, and you can define your own
shaperange to contain what shapes you like - you could even use each
shape's TopLeftCell and BottomRightCell properties to help you decide
which shapes to include.
The following bit of code puts the centre of each shape on the active
sheet on the centre of its TopLeftCell (a process which could well
change the TopLeftCell property of the shape, especially if the shape is
bigger than that cell):Sub blah()
For Each shp In ActiveSheet.Shapes
Set xxx = shp.TopLeftCell
shp.Top = xxx.Top + xxx.Height / 2 - shp.Height / 2
shp.Left = xxx.Left + xxx.Width / 2 - shp.Width / 2
Next shp
End Sub
and a way, perhaps, of narrowing down just which shapes you
want to move around:Sub blah()
For Each shp In ActiveSheet.Shapes
If Not Intersect(shp.TopLeftCell, Range("E11:BF100")) Is Nothing
Then
Set xxx = shp.TopLeftCell
shp.Top = xxx.Top + xxx.Height / 2 - shp.Height / 2
shp.Left = xxx.Left + xxx.Width / 2 - shp.Width / 2
End If
Next shp
End Sub


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=134999

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Position shapes to be centered in a cell

Centering the pictures in a cell doesn't mean too much to me.

You could have the picture fill up the cell with no border--or have a large
border and a very small picture--or anything in between.

When I'm doing this kind of thing, I usually want the picture to fill the cell
(and not go outside the cell). That means I can shrink the height and width
(preservinging the aspect ratio, too).

Either way, this may give you a way to experiment with what you want:

Option Explicit
Sub testme()
Dim myPict As Picture
Dim myCell As Range
Dim myRngToInspect As Range
Dim myAspectRatio As Double

With ActiveSheet
Set myRngToInspect = .Range("E11:E20") 'E11:BF100 after finished testing

For Each myPict In .Pictures
Set myCell = myPict.TopLeftCell
If Intersect(myCell, myRngToInspect) Is Nothing Then
'outside that range, skip it
Else
With myPict
myAspectRatio = .Width / .Height
.ShapeRange.LockAspectRatio = msoTrue

.Left = myCell.Left
.Top = myCell.Top
.Height = myCell.Height
.Width = myAspectRatio * .Height
If .Width myCell.Width Then
'too wide for the cell
'With the aspectratio locked, the
'reducing the width will reduce the height
.Width = myCell.Width
End If
End With
End If
Next myPict
End With

End Sub


James wrote:

Hi, Ive got a question about how to re-position a shape (type 13).

Ive got a spreadsheet that has a bunch of little pictures nested each in its
own cell. what im trying to do is center the picture in the center of the
cell. Im getting an error on "selection.shaperange.incrementtop 10" what am i
doing wrong?

any help would be great, thanks in advance

For Each C In Activesheet.Range("E11:BF100")
C.Select
On Error Resume Next 'if no shape in that cell
Selection.ShapeRange.IncrementTop 10
Selection.ShapeRange.IncrementLeft -1
On Error GoTo 0
Next C


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 542
Default Position shapes to be centered in a cell

Thank you for the help. p45cal that method worked great. I do have 2003 and
probably the reason for the error. thanks again!

"p45cal" wrote:


Maybe you have Excel 2007 and it allows you to select shapes on a sheet
in this way (Selection.ShapeRange (ie. a range of cells on a sheet
doesn't have its own range of shapes)). Not in Excel 2003.
A sheet has its collection of shapes, and you can define your own
shaperange to contain what shapes you like - you could even use each
shape's TopLeftCell and BottomRightCell properties to help you decide
which shapes to include.
The following bit of code puts the centre of each shape on the active
sheet on the centre of its TopLeftCell (a process which could well
change the TopLeftCell property of the shape, especially if the shape is
bigger than that cell):Sub blah()
For Each shp In ActiveSheet.Shapes
Set xxx = shp.TopLeftCell
shp.Top = xxx.Top + xxx.Height / 2 - shp.Height / 2
shp.Left = xxx.Left + xxx.Width / 2 - shp.Width / 2
Next shp
End Sub
and a way, perhaps, of narrowing down just which shapes you
want to move around:Sub blah()
For Each shp In ActiveSheet.Shapes
If Not Intersect(shp.TopLeftCell, Range("E11:BF100")) Is Nothing
Then
Set xxx = shp.TopLeftCell
shp.Top = xxx.Top + xxx.Height / 2 - shp.Height / 2
shp.Left = xxx.Left + xxx.Width / 2 - shp.Width / 2
End If
Next shp
End Sub


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=134999


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making the text in a cell with a money format centered Wombat Excel Discussion (Misc queries) 6 April 22nd 23 12:12 AM
Changing position of shapes in chart Matt S[_2_] Excel Discussion (Misc queries) 1 March 25th 10 12:14 PM
checkbox centered in cell ranswert Excel Programming 4 March 11th 08 12:45 PM
Adding Shapes: Position changes when running code a second time - Why? Richard Buttrey Excel Programming 3 February 21st 08 12:20 PM
Cell alignment centered Simon Fischer Excel Programming 1 July 21st 05 11:01 AM


All times are GMT +1. The time now is 03:08 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"