Trouble with Shapes

dreid1011

Well-known Member
Joined
Jun 4, 2015
Messages
3,085
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

In an attempt to solve another's problem, I am playing with shapes. I am trying to detect overlapping shapes created through vba on demand. I have a list of x and y coordinates and convert those to a position on a chart.

The problem I am running into at the moment, is that two shapes with the same (x, y) values do not have identical .Left values and it's boggling my mind.

The code is placing them at exactly the same place, but when I Debug.Print the values for .Left of each one, they show completely different numbers. This of course, is throwing off any attempt to detect if the shapes are overlapping.

Rich (BB code):
Private Sub InsertTextBoxes()

Dim tB1 As Shape, tB2 As Shape
Dim ws As Worksheet
Dim i As Long, j As Long, k As Long
Dim xVal As Variant, yVal As Variant

Set ws = Worksheets("Sheet1")

j = ws.Range("C" & Rows.Count).End(xlUp).Row

xVal = Split("0,40,32,24,16,8", ",")
yVal = Split("0,L,P,T,X,AB", ",")

For i = 5 To j
Set tB1 = ws.Shapes.AddTextbox(msoTextOrientationHorizontal, Range(yVal(Range("D" & i).Value) & xVal(Range("C" & i).Value)).Left + 15, Range(yVal(Range("D" & i).Value) & xVal(Range("C" & i).Value)).Top + 7, 30, 15)
tB1.TextFrame.Characters.Text = ws.Range("B" & i).Address(0, 0)
tB1.TextFrame.HorizontalAlignment = xlHAlignCenter
tB1.TextFrame.VerticalAlignment = xlVAlignCenter
tB1.TextFrame.Characters.Font.Size = 8

If i = 29 Then
    Debug.Print "X"
    Debug.Print tB1.Left
    Debug.Print ws.Shapes(24).Left & " 24"
    Debug.Print ws.Shapes(25).Left & " 25"
    If tB1.Left = ws.Shapes(i - 5).Left Then
        Debug.Print "Y"
'        tB1.IncrementLeft -31
'        tB1.IncrementTop -16
    End If
End If
    

Next i

End Sub

The highlighted portion is where I am attempting to adjust for overlaps.. Of course, it's not complete at the moment as I was just trying to identify why it wasn't working. (I've changed/deleted some code lines..)

Anyway, when i = 29, tB1 is the last shape created and should have identical .left and .top values to the previous shape as both their (x, y) values are (3, 3) and thus placed at the same point. tB1 and ws.Shapes(24) and ws.Shapes(25) should have the same values.

But this is what I see from debug.print:

tB1.Left = 842.25
ws.Shapes(24).Left = 1640.25 24
ws.Shapes(25).Left = 962.25 25

I'm very confused.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
On further inspection, it would seem tB1 is not the same shape as ws.Shapes(25) when i = 29. If I manually select and delete all the shapes on my sheet, does it reset the shapes array when I recreate the shapes the next time?
 
Upvote 0
And now the problem is gone. I pressed F5 > Special > Objects > Delete. It's all working as expected now.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

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