Home » excel » Excel VBA: Height of shapes change after copy to sheet

Excel VBA: Height of shapes change after copy to sheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

I encountered this weird phenonemon when copying shapes with VBA, and I don’t know how to deal with this:

When Cells and Shapes are copied using the regular .Copy function, the shapes are slighty compressed in the height direction. Their placement seems right, more or less.

I use VBA 7.0 with Excel 2010.

I sincerely hope someone can help me with this. I spend quite some time finding info on this subject!

Code:

Sub RenderOverview()

' Clear Worksheet
Sheets("ModelOverzicht").Cells.Clear
Sheets("ModelOverzicht").DrawingObjects.Delete

    ' Copy SINGLE SECTION
    Sheets("Enkele Sectie").Range("A1:AM29").Copy
    Sheets("ModelOverzicht").Select
    Range("A1").Select
    ActiveSheet.Paste
End Sub
How to&Answers:

I came here, because I had the same problem and the answer of “user1759942” didn’t solve my problem. After a little bit investigation I was able to nail down the cause of the change in size of my chart.

My origin worksheet (where I copy said chart) had a zoom of 80%, while my destination worksheet (where I paste copied chart) had no zoom (100%).
After changing both worksheets to the same zoom value (80%) and activating the destination worksheet right before pasting the copied chart, the problem was gone. I didn’t even need to change the “Format Shape” – Properties – “move but dont size” like suggested by “user1759942” (by the way: this is done in VBA/VBscript by setting: [Worksheet].ChartObjects(1).Placement = xlMove ‘ xlMove = &H2).

Perhaps this is an answer to question of “user3250519”.

Answer:

this is because the shapes are tied to the cells By default. So if a cell’s height / width is changed, so will the shapes height / width. Right click “Format Shape” – Properties – “move but dont size” will keep the shapes spot, but will not change the size. Try it out. you may have to resort to savign the height of the rows / shapes before pasting and apply that after pasting

Edit

When copying and pasting, cells heights / widths are changed to I think the height and width of the range they are being pasted to. You could also change the height and width of the target range, to match the source range, before pasting.