Resize an image in Excel (VBA) using cells

Solved
STVNA Posted messages 32 Status Member -  
STVNA Posted messages 32 Status Member -
Hello everyone!

In 2015, I posted this question: https://forums.commentcamarche.net/forum/affich-31772094-redimensionner-une-image-automatiquement-avec-excel-vba

We would now like to resize an image in Excel in such a way that when we enter a width and a height in cells, an image automatically resizes according to these values using VBA.

Thank you in advance!

1 answer

danielc0 Posted messages 2173 Registration date   Status Member Last intervention   285
 
Hello,

Assuming there is only one image on the sheet, put this macro in the sheet module:

Private Sub Worksheet_Change(ByVal Target As Range) If [A1] <> "" And [A2] <> "" And IsNumeric([A1]) And IsNumeric([A2]) Then If Target.Address = "$a$1" Or Target.Address = "$a$2" Then With ActiveSheet.Pictures(1) .Height = [A1] .Width = [A2] End With End If End If End Sub


A1=height
A2=width
With the values in points.

Best regards.
0
STVNA Posted messages 32 Status Member
 
Hello,
Thank you for your reply!
But it doesn’t work. Where exactly should we place the macro?
0
danielc0 Posted messages 2173 Registration date   Status Member Last intervention   285 > STVNA Posted messages 32 Status Member
 
Don't turn things around. If you don't know where to place the macro, you can't say it doesn't work.
Right-click on the tab of the sheet containing the image and click on "View Code". Paste the macro into the right side of the VBE window.

Daniel
0
STVNA Posted messages 32 Status Member
 
This is exactly what we did. Do we need to rename the tab or the image?
And what do you mean by putting the values in points?
Thank you in advance.
0
danielc0 Posted messages 2173 Registration date   Status Member Last intervention   285 > STVNA Posted messages 32 Status Member
 
No need to rename the image. However, there should only be one on the sheet.
The point is worth 1/72 of an inch and an inch is 2.54 cm. The workbook must be saved in .xlsm format and macros must be enabled. If you still have a problem, upload your workbook to mon-partage.fr and post the obtained link.

Daniel
0
STVNA Posted messages 32 Status Member
 
Désolé, je ne peux pas accéder aux liens ou fichiers externes.
0