insert picture in cell and resize option ON

drom

Well-known Member
Joined
Mar 20, 2005
Messages
543
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and thanks in advance!

I am inserting a image located in a webside in a cell

VBA Code:
dim rCell as range:                           Set rCell=ActiveCell
dim wURL as string:                           wURL="https://logodix.com/logo/701195.jpg"  'Just for this eg
        Set PIC = Nothing:                    Set PIC = ActiveSheet.Pictures.Insert(wURL)
        If PIC Is Nothing Then
          xPicWrong = xPicWrong + 1
        Else
          xPicOK = xPicOK + 1
          With PIC.ShapeRange
            .LockAspectRatio = msoFalse
            .Top = rCell.Top
            .Left = rCell.Left
            If .Width > rCell.Width Then
              .Width = rCell.Width * 5 / 7
              .Left = rCell.Left + (rCell.Width * 1 / 7)
            End If
            If .Height > rCell.Height Then
              .Height = rCell.Height * 5 / 7
              .Top = rCell.Top + (rCell.Height * 1 / 7)
            End If
          End With
          PIC.Placement = xlMoveAndSize
        End If
      End If

The picture goes perfect, but if for any reason I increase the column Width or the row height, the picture keeps the previous size, does no resize accordingly

What am I missing ?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
This is an old function I made that always seemed to work.

VBA Code:
Private Sub InsertPic(filePath As String, ByVal insertCell As Range)
    Dim xlShapes As Shapes
    Dim xlPic As Shape
    Dim xlWorksheet As Worksheet

    If IsEmpty(filePath) Or Len(Dir(filePath)) = 0 Then
        MsgBox ("File Path invalid")
        Exit Sub
    End If

    Set xlWorksheet = ActiveSheet
    
    Set xlPic = xlWorksheet.Shapes.AddPicture(filePath, msoFalse, msoCTrue, insertCell.Left, insertCell.Top, insertCell.Width, insertCell.Height)
    xlPic.Placement = xlMoveAndSize
    xlPic.LockAspectRatio = msoCTrue
End Sub
 
Upvote 0
OK, looks like when the picture keeps the cell's size works fine
But if the picture is a bit smaller is there any way of getting the same
Thanks again
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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