Inserting Picture (macro) - size and position

Spaztic

New Member
Joined
Jul 27, 2023
Messages
30
Office Version
  1. 365
Platform
  1. Windows
This seems challenging. I'm looking for a macro to run that will:
  1. Insert a picture (would bring the user to the screen where they search for their picture)
  2. Place the picture in the cell they already had selected (e.g. B2 below)
  3. Size/scale the picture automatically to fully fit within the cell
    • Preferred to leave gaps around all 4 edges of the picture to the cells but not a show stopper (see below)...centered?
  4. Picture must 'move and size with cells'

With the code I've tried (below), the struggles I've had are:
  • Pictures (in horizontal orientation (e.g. B2)) are filling the height of the cell (they go beyond the width)
  • Pictures (in the vertical orientation (e.g. B4)) are sizing the top edge as the height of the cell and placing the picture somewhere else in the worksheet
    • NOTE: If I insert a vertical picture using the conventional methods, it goes in the selected cell.

VBA Code:
Sub InsertPic()
Dim fNameAndPath As Variant
Dim img As Picture
fNameAndPath = Application.GetOpenFilename
    Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
    With img
       .Left = ActiveCell.Left
       .Top = ActiveCell.Top
       .Width = ActiveCell.Width
       .Height = ActiveCell.Height
       .Placement = 1
       .PrintObject = True
    End With
    
End Sub

1704811344309.png

 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I have amended your code as follows...

VBA Code:
Sub InsertPic()

    Dim fNameAndPath As Variant
    Dim rng As Range
    Dim img As Picture
  
    fNameAndPath = Application.GetOpenFilename( _
        FileFilter:="Image Files (*.gif;*.jpg;*.png), *.gif;*.jpg;*.png", _
        Title:="Select an Image", _
        ButtonText:="Select")
      
    If fNameAndPath = False Then Exit Sub
  
    Set rng = ActiveCell
  
    Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
    With img
        If .Width > .Height Then
            .Width = rng.Width * 0.8
        Else
            .Height = rng.Height * 0.8
        End If
        .Left = rng.Left + (rng.Width - .Width) / 2
        .Top = rng.Top + (rng.Height - .Height) / 2
       .Placement = 1
       .PrintObject = True
    End With
  
End Sub

You'll notice that I have added a filter when prompted to select an image. You can add to or change the filter as desired. Also, you'll notice that the code exits the sub when the user is prompted to select an image, and clicks on Cancel.

Hope this helps!
 
Upvote 0
Solution
I have amended your code as follows...

VBA Code:
Sub InsertPic()

    Dim fNameAndPath As Variant
    Dim rng As Range
    Dim img As Picture
 
    fNameAndPath = Application.GetOpenFilename( _
        FileFilter:="Image Files (*.gif;*.jpg;*.png), *.gif;*.jpg;*.png", _
        Title:="Select an Image", _
        ButtonText:="Select")
     
    If fNameAndPath = False Then Exit Sub
 
    Set rng = ActiveCell
 
    Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
    With img
        If .Width > .Height Then
            .Width = rng.Width * 0.8
        Else
            .Height = rng.Height * 0.8
        End If
        .Left = rng.Left + (rng.Width - .Width) / 2
        .Top = rng.Top + (rng.Height - .Height) / 2
       .Placement = 1
       .PrintObject = True
    End With
 
End Sub

You'll notice that I have added a filter when prompted to select an image. You can add to or change the filter as desired. Also, you'll notice that the code exits the sub when the user is prompted to select an image, and clicks on Cancel.

Hope this helps!
Amazing! Thank you so much! This was exactly what I was looking for and worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,992
Members
449,094
Latest member
masterms

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