Bug? Aligning Shapes to Cell dimensions is now not and exact match?

vbavirgin

New Member
Joined
Oct 5, 2011
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a macro that fills a textbox with a picture from a URL and aligns the textbox to a cell.

VBA Code:
Sub FillPic()

Dim i As Long, URL As String
Dim oTextBox As TextBox

For Each oTextBox In ActiveSheet.TextBoxes
    oTextBox.Delete
Next oTextBox
  
    i = 2
    Do While Len(ActiveSheet.Cells(i, 1).Text) > 0

    URL = ActiveSheet.Cells(i, 1).Text

        With ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 50)
            .Left = ActiveSheet.Cells(i, 2).Left
            .Top = ActiveSheet.Cells(i, 2).Top
            .Height = ActiveSheet.Cells(i, 2).Height
            .Width = ActiveSheet.Cells(i, 2).Width
            .Fill.UserPicture URL
            On Error Resume Next
        End With
      
        i = i + 1
    Loop

End Sub

The issue I have is that the alignment is not 100% exact so that all the textboxes are created row by row they start to align out of sync with the cells (see image).

This never used to be the case when using an older version of excel but is now an annoying issue.!!

Has anyone else noticed this? Can somebody help me with this so that the textboxes align exactly with the cells?

alignment.png




Thanks,

Lewis
 
Last edited:
The easiest way would just be to hold alt and resize the picture and see if it will change size to match the cell. Alternatively you could put something like this in your code temporarily to compare them:

VBA Code:
MsgBox "Shape top: " & .Top & vbNewLine & "Cell top: " & ActiveSheet.Cells(i, 2).Top
If I run this in my code after .top, the first cell and shape .top match and then they differ after that (hence the misalignment)
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The easiest way would just be to hold alt and resize the picture and see if it will change size to match the cell. Alternatively you could put something like this in your code temporarily to compare them:

VBA Code:
MsgBox "Shape top: " & .Top & vbNewLine & "Cell top: " & ActiveSheet.Cells(i, 2).Top
This shows me that the tops are indeed not matching
 
Upvote 0
Just out of curiosity, why are you using textboxes, not pictures?

Maybe try positioning it on the cell to start with:

Code:
        ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveSheet.Cells(i, 2).Left, ActiveSheet.Cells(i, 2).Top, ActiveSheet.Cells(i, 2).Width, ActiveSheet.Cells(i, 2).Height).Fill.UserPicture URL
 
Upvote 0
Just out of curiosity, why are you using textboxes, not pictures?

Maybe try positioning it on the cell to start with:

Code:
        ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveSheet.Cells(i, 2).Left, ActiveSheet.Cells(i, 2).Top, ActiveSheet.Cells(i, 2).Width, ActiveSheet.Cells(i, 2).Height).Fill.UserPicture URL
Hi RoryA,

I was using a textbox as I was also adding text to the pictures form other cells (in another project).

I tried setting the shape size at creation but it still results in the misalignment! Thanks though.
 
Upvote 0
If I run this in my code after .top, the first cell and shape .top match and then they differ after that (hence the misalignment)
Could be worth adding another .Top as the last line and seeing if that has any effect? If not, I assume that something is moving it in a different iteration of the loop. In which case, changing the placement property may have an effect.

VBA Code:
        With ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 50)
            .Fill.UserPicture URL
            On Error Resume Next
            .LockAspectRatio = False
            .Placement = xlFreeFloating
            .Height = ActiveSheet.Cells(i, 2).Height
            .Width = ActiveSheet.Cells(i, 2).Width
            .Left = ActiveSheet.Cells(i, 2).Left
            .Top = ActiveSheet.Cells(i, 2).Top
        End With
 
Upvote 0
Could be worth adding another .Top as the last line and seeing if that has any effect? If not, I assume that something is moving it in a different iteration of the loop. In which case, changing the placement property may have an effect.

VBA Code:
        With ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 50)
            .Fill.UserPicture URL
            On Error Resume Next
            .LockAspectRatio = False
            .Placement = xlFreeFloating
            .Height = ActiveSheet.Cells(i, 2).Height
            .Width = ActiveSheet.Cells(i, 2).Width
            .Left = ActiveSheet.Cells(i, 2).Left
            .Top = ActiveSheet.Cells(i, 2).Top
        End With
No joy i'm affraid but thank you for the help!

If I add a message box to report the shape vs cell, I can see that the code is not setting the height, top or width the same as the cell. Only .left matches through the iterations
 
Upvote 0
No joy i'm affraid but thank you for the help!

If I add a message box to report the shape vs cell, I can see that the code is not setting the height, top or width the same as the cell. Only .left matches through the iterations
Weird. Did you say that the top is correct when you change it initially, but then changes afterwards? If so, the best way forward would be to identify which line is changing it.
If it's not even changing when you first set the top property, I would give it a go without the on error statement and see if maybe there is some error being triggered? Hard to tell without seeing the file though.
 
Upvote 0
I am 99% certain the issue lies with .Top
I will read up on this - do you have an understanding of this range object?
 
Upvote 0
I have found a backwards solution to my problem using comment boxes! It is not ideal but it works... I got the idea form Excel VBA: Fill.UserPicture. An added benefit is that you can also make them hidden until the cell is hovered over.

Still does not explain when .Top and .Height is not working in my original code

VBA Code:
Sub FillPicComment1()
Dim URL As String
Dim rng As Range
Dim oTextBox As TextBox

For Each oTextBox In ActiveSheet.TextBoxes
oTextBox.Delete
Next oTextBox

    i = 2
    Do While Len(ActiveSheet.Cells(i, 1).Text) > 0

    URL = ActiveSheet.Cells(i, 1).Text

    Set rng = ActiveSheet.Cells(i, 2)
    On Error Resume Next
    rng.AddComment
        With rng.Comment
        '.Shape.TextFrame.AutoSize = True
        .Shape.Placement = xlMoveAndSize
        .Visible = True
        .Shape.Top = .Parent.Top
        .Shape.Left = .Parent.Left
        .Shape.Height = .Parent.Height
        .Shape.Width = .Parent.Width
        .Shape.Line.Visible = msoTrue
    End With
    'On Error Resume Next
    'rng.Comment.Text Text:=" "
    rng.Comment.Shape.Fill.UserPicture (URL)
    i = i + 1
    Loop

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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