Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Adding shapes to worksheet

Hi,
I have the following code

Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) < "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
Set p = Nothing

End Sub

This works just fine. However, If I add more than one shape to a sheet,
then subsequently delete a shape using the code below, other shapes on the
same worksheet move a little.

Sub DeletePicture(TargetCells As Range)
Dim pict As Object
Dim t As Double
Dim l As Double

Application.ScreenUpdating = False
' determine positions
With TargetCells
t = .Top
l = .Left
End With

For Each pict In ActiveSheet.Shapes
On Error Resume Next
pict.Select
If Round(pict.Left, 2) = Round(l, 2) And Round(pict.Top, 2) =
Round(t, 2) Then
pict.Delete
End If
Next

Application.ScreenUpdating = True

End Sub

Any suggestions to improve the code, so that each shape is "locked in place"
when it is added ?

Thanks in advance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Adding shapes to worksheet


Try maximize your excel window and the spreadsheet inside of excel.
Your code isn't moving anything so I think the actual window is
resizing.


--
joel
------------------------------------------------------------------------
joel's Profile: http://www.thecodecage.com/forumz/member.php?userid=229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=157392

Microsoft Office Help

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Adding shapes to worksheet

No need to Select your pictures. Here's a different approach -

Sub DeletePicture2(TargetCells As Range)
Dim pic As Picture

For Each pic In ActiveSheet.Pictures
If Not Intersect(TargetCells, pic.TopLeftCell) Is Nothing Then
pic.Delete
End If
Next

End Sub

If You want to delete all shapes whose topLeftCell is in the target change
'As Picture' to As Shape' and 'ActiveSheet.Pictures' to 'ActiveSheet.Shapes'

Regards,
Peter T


"Gary B" wrote in message
...
Hi,
I have the following code

Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) < "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
Set p = Nothing

End Sub

This works just fine. However, If I add more than one shape to a sheet,
then subsequently delete a shape using the code below, other shapes on the
same worksheet move a little.

Sub DeletePicture(TargetCells As Range)
Dim pict As Object
Dim t As Double
Dim l As Double

Application.ScreenUpdating = False
' determine positions
With TargetCells
t = .Top
l = .Left
End With

For Each pict In ActiveSheet.Shapes
On Error Resume Next
pict.Select
If Round(pict.Left, 2) = Round(l, 2) And Round(pict.Top, 2) =
Round(t, 2) Then
pict.Delete
End If
Next

Application.ScreenUpdating = True

End Sub

Any suggestions to improve the code, so that each shape is "locked in
place"
when it is added ?

Thanks in advance.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Adding shapes to worksheet

Thanks for that.

Your code did not delete my shape, but I did remove my line that was
selecting the shape before deletion. That has worked a treat - simply don't
select the shape.

Your guidance was most helpful.


"Peter T" wrote:

No need to Select your pictures. Here's a different approach -

Sub DeletePicture2(TargetCells As Range)
Dim pic As Picture

For Each pic In ActiveSheet.Pictures
If Not Intersect(TargetCells, pic.TopLeftCell) Is Nothing Then
pic.Delete
End If
Next

End Sub

If You want to delete all shapes whose topLeftCell is in the target change
'As Picture' to As Shape' and 'ActiveSheet.Pictures' to 'ActiveSheet.Shapes'

Regards,
Peter T


"Gary B" wrote in message
...
Hi,
I have the following code

Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) < "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
Set p = Nothing

End Sub

This works just fine. However, If I add more than one shape to a sheet,
then subsequently delete a shape using the code below, other shapes on the
same worksheet move a little.

Sub DeletePicture(TargetCells As Range)
Dim pict As Object
Dim t As Double
Dim l As Double

Application.ScreenUpdating = False
' determine positions
With TargetCells
t = .Top
l = .Left
End With

For Each pict In ActiveSheet.Shapes
On Error Resume Next
pict.Select
If Round(pict.Left, 2) = Round(l, 2) And Round(pict.Top, 2) =
Round(t, 2) Then
pict.Delete
End If
Next

Application.ScreenUpdating = True

End Sub

Any suggestions to improve the code, so that each shape is "locked in
place"
when it is added ?

Thanks in advance.



.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Adding shapes to worksheet

I just tested the code as posted and it should delete any or all Pictures
whose topLeftCell is within the target range "TargetCells". Not sure why it
doesn't for you, no matter, glad you got it working for your needs.

Regards,
Peter T

"Gary B" wrote in message
...
Thanks for that.

Your code did not delete my shape, but I did remove my line that was
selecting the shape before deletion. That has worked a treat - simply
don't
select the shape.

Your guidance was most helpful.


"Peter T" wrote:

No need to Select your pictures. Here's a different approach -

Sub DeletePicture2(TargetCells As Range)
Dim pic As Picture

For Each pic In ActiveSheet.Pictures
If Not Intersect(TargetCells, pic.TopLeftCell) Is Nothing Then
pic.Delete
End If
Next

End Sub

If You want to delete all shapes whose topLeftCell is in the target
change
'As Picture' to As Shape' and 'ActiveSheet.Pictures' to
'ActiveSheet.Shapes'

Regards,
Peter T


"Gary B" wrote in message
...
Hi,
I have the following code

Sub InsertPictureInRange(PictureFileName As String, TargetCells As
Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) < "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
.Width = w
.Height = h
End With
Set p = Nothing

End Sub

This works just fine. However, If I add more than one shape to a
sheet,
then subsequently delete a shape using the code below, other shapes on
the
same worksheet move a little.

Sub DeletePicture(TargetCells As Range)
Dim pict As Object
Dim t As Double
Dim l As Double

Application.ScreenUpdating = False
' determine positions
With TargetCells
t = .Top
l = .Left
End With

For Each pict In ActiveSheet.Shapes
On Error Resume Next
pict.Select
If Round(pict.Left, 2) = Round(l, 2) And Round(pict.Top, 2) =
Round(t, 2) Then
pict.Delete
End If
Next

Application.ScreenUpdating = True

End Sub

Any suggestions to improve the code, so that each shape is "locked in
place"
when it is added ?

Thanks in advance.



.





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
adding formulas to shapes in excel A Excel Discussion (Misc queries) 2 May 15th 23 11:43 AM
Adding Shapes to Spreadsheets Buckeye Bill Excel Discussion (Misc queries) 1 May 8th 08 08:17 PM
adding formulas to shapes in excel A Excel Discussion (Misc queries) 0 February 22nd 08 01:14 PM
adding shapes to forms gbpg Excel Programming 6 November 27th 07 09:34 AM
Adding shapes to xy scatter charts Richw Excel Programming 2 October 14th 05 04:15 PM


All times are GMT +1. The time now is 02:57 AM.

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

About Us

"It's about Microsoft Excel"